The Witchcraft Compiler Collection
WCC
 All Data Structures Files Functions Variables Typedefs Macros
wld.c
Go to the documentation of this file.
1 
10 #include <fcntl.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <sys/mman.h>
14 #include <sys/stat.h>
15 #include <unistd.h>
16 #include <fcntl.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <sys/stat.h>
20 #include <sys/types.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <limits.h>
26 #include <errno.h>
27 #include <elf.h>
28 
29 #include <config.h>
30 
31 #define DEFAULT_NAME "wld"
32 
36 int mk_lib(char *name)
37 {
38  int fd;
39  struct stat sb;
40  char *map = 0;
41  Elf32_Ehdr *ehdr32;
42  Elf64_Ehdr *ehdr64;
43 
44  fd = open(name, O_RDWR);
45  if (fd <= 0) {
46  printf(" !! couldn't open %s : %s\n", name, strerror(errno));
47  exit(EXIT_FAILURE);
48  }
49 
50  if (fstat(fd, &sb) == -1) {
51  printf(" !! couldn't stat %s : %s\n", name, strerror(errno));
52  exit(EXIT_FAILURE);
53  }
54 
55  if ((unsigned int) sb.st_size < sizeof(Elf32_Ehdr)) {
56  printf(" !! file %s is too small (%u bytes) to be a valid ELF.\n", name, (unsigned int) sb.st_size);
57  exit(EXIT_FAILURE);
58  }
59 
60  map = mmap(NULL, sb.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
61  if (map == MAP_FAILED) {
62  printf(" !! couldn't mmap %s : %s\n", name, strerror(errno));
63  exit(EXIT_FAILURE);
64  }
65 
66  switch (map[EI_CLASS]) {
67  case ELFCLASS32:
68  ehdr32 = (Elf32_Ehdr *) map;
69  ehdr32->e_type = ET_DYN;
70  break;
71  case ELFCLASS64:
72  ehdr64 = (Elf64_Ehdr *) map;
73  ehdr64->e_type = ET_DYN;
74  break;
75  default:
76  printf(" !! unknown ELF class\n");
77  exit(EXIT_FAILURE);
78  }
79 
80  munmap(map, sb.st_size);
81  close(fd);
82  return 0;
83 }
84 
85 int print_version(void)
86 {
87  printf("%s version:%s (%s %s)\n", WNAME, WVERSION, WTIME, WDATE);
88  return 0;
89 }
90 
91 int main(int argc, char **argv)
92 {
93 
94  if ((argc < 2)||(strncmp(argv[1],"-libify",7))) {
95  print_version();
96  printf("\nUsage: %s [options] file\n", argc > 0 ? argv[0] : DEFAULT_NAME);
97  printf("\noptions:\n\n -libify Set Class to ET_DYN in input ELF file.\n\n");
98  exit(EXIT_FAILURE);
99  }
100 
101  mk_lib(argv[2]);
102 
103  return 0;
104 }
int print_version(void)
Definition: wld.c:85
void exit(int status)
Definition: wsh.c:3137
int map(lua_State *L)
Definition: wsh.c:3658
int mk_lib(char *name)
Definition: wld.c:36
int main(int argc, char **argv)
Definition: wld.c:91
#define DEFAULT_NAME
Definition: wld.c:31