annotate elf.c @ 2:7bf4a164d5bb default tip

fix bug: long_options have to be zero terminated
author Peter Meerwald <p.meerwald@bct-electronic.com>
date Fri, 20 Jul 2012 11:28:30 +0200
parents bbbfb3f97919
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
1
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
2 #ifdef HAVE_CONFIG_H
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
3 # include "config.h"
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
4 #endif
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
5
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
6 #include <elf.h>
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
7 #if defined(HAVE_SYS_LINK_H)
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
8 # include <sys/link.h> /* Find DT_RPATH on Solaris 2.6 */
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
9 #endif /* HAVE_SYS_LINK_H */
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
10 #include <stdio.h>
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
11 #include <string.h>
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
12 #include <unistd.h>
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
13 #include <sys/types.h>
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
14 #include <sys/stat.h>
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
15 #include <errno.h>
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
16 #include <fcntl.h>
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
17 #include "protos.h"
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
18
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
19 int
1
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
20 elf32_open(const char *filename, int flags, Elf32_Ehdr *ehdr)
0
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
21 {
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
22 int fd;
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
23
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
24 fd = open(filename, flags);
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
25 if (fd == -1)
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
26 {
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
27 perror ("open");
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
28 return -1;
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
29 }
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
30
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
31 if (read(fd, ehdr, sizeof(*ehdr)) != sizeof(*ehdr))
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
32 {
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
33 perror ("reading header");
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
34 close(fd);
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
35 return -1;
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
36 }
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
37
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
38 if (0 != memcmp(ehdr->e_ident, ELFMAG, SELFMAG) ||
1
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
39 ehdr->e_ident[EI_CLASS] != ELFCLASS32 ||
0
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
40 ehdr->e_ident[EI_DATA] != ELFDATA2 ||
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
41 ehdr->e_ident[EI_VERSION] != EV_CURRENT)
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
42 {
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
43 fprintf(stderr,
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
44 #ifdef WORDS_BIGENDIAN
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
45 "`%s' probably isn't a %d-bit MSB-first ELF file.\n",
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
46 #else /* not WORD_BIGENDIAN */
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
47 "`%s' probably isn't a %d-bit LSB-first ELF file.\n",
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
48 #endif /* not WORD_BIGENDIAN */
1
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
49 filename, 32);
0
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
50 close(fd);
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
51 errno = ENOEXEC; /* Hm, is this the best errno code to use? */
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
52 return -1;
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
53 }
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
54
1
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
55 printf("32-bit ELF file\n");
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
56
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
57 if (ehdr->e_phentsize != sizeof(Elf32_Phdr))
0
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
58 {
1
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
59 fprintf(stderr, "section size was read as %d, not %lu!\n",
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
60 ehdr->e_phentsize, sizeof(Elf32_Phdr));
0
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
61 close(fd);
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
62 return -1;
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
63 }
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
64 return fd;
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
65 }
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
66
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
67 int
1
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
68 elf64_open(const char *filename, int flags, Elf64_Ehdr *ehdr)
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
69 {
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
70 int fd;
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
71
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
72 fd = open(filename, flags);
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
73 if (fd == -1)
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
74 {
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
75 perror ("open");
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
76 return -1;
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
77 }
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
78
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
79 if (read(fd, ehdr, sizeof(*ehdr)) != sizeof(*ehdr))
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
80 {
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
81 perror ("reading header");
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
82 close(fd);
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
83 return -1;
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
84 }
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
85
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
86 if (0 != memcmp(ehdr->e_ident, ELFMAG, SELFMAG) ||
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
87 ehdr->e_ident[EI_CLASS] != ELFCLASS64 ||
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
88 ehdr->e_ident[EI_DATA] != ELFDATA2 ||
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
89 ehdr->e_ident[EI_VERSION] != EV_CURRENT)
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
90 {
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
91 fprintf(stderr,
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
92 #ifdef WORDS_BIGENDIAN
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
93 "`%s' probably isn't a %d-bit MSB-first ELF file.\n",
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
94 #else /* not WORD_BIGENDIAN */
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
95 "`%s' probably isn't a %d-bit LSB-first ELF file.\n",
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
96 #endif /* not WORD_BIGENDIAN */
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
97 filename, 64);
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
98 close(fd);
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
99 errno = ENOEXEC; /* Hm, is this the best errno code to use? */
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
100 return -1;
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
101 }
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
102
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
103 printf("64-bit ELF file\n");
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
104
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
105 if (ehdr->e_phentsize != sizeof(Elf64_Phdr))
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
106 {
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
107 fprintf(stderr, "section size was read as %d, not %lu!\n",
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
108 ehdr->e_phentsize, sizeof(Elf64_Phdr));
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
109 close(fd);
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
110 return -1;
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
111 }
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
112 return fd;
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
113 }
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
114
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
115 int
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
116 elf32_find_dynamic_section(int fd, Elf32_Ehdr *ehdr, Elf32_Phdr *phdr)
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
117 {
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
118 int i;
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
119 if (lseek(fd, ehdr->e_phoff, SEEK_SET) == -1)
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
120 {
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
121 perror ("positioning for sections");
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
122 return 1;
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
123 }
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
124
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
125 for (i = 0; i < ehdr->e_phnum; i++)
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
126 {
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
127 if (read(fd, phdr, sizeof(*phdr)) != sizeof(*phdr))
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
128 {
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
129 perror ("reading section header");
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
130 return 1;
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
131 }
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
132 if (phdr->p_type == PT_DYNAMIC)
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
133 break;
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
134 }
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
135 if (i == ehdr->e_phnum)
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
136 {
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
137 fprintf (stderr, "No dynamic section found.\n");
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
138 return 2;
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
139 }
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
140
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
141 if (0 == phdr->p_filesz)
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
142 {
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
143 fprintf (stderr, "Length of dynamic section is zero.\n");
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
144 return 3;
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
145 }
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
146
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
147 return 0;
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
148 }
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
149
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
150 int
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
151 elf64_find_dynamic_section(int fd, Elf64_Ehdr *ehdr, Elf64_Phdr *phdr)
0
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
152 {
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
153 int i;
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
154 if (lseek(fd, ehdr->e_phoff, SEEK_SET) == -1)
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
155 {
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
156 perror ("positioning for sections");
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
157 return 1;
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
158 }
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
159
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
160 for (i = 0; i < ehdr->e_phnum; i++)
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
161 {
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
162 if (read(fd, phdr, sizeof(*phdr)) != sizeof(*phdr))
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
163 {
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
164 perror ("reading section header");
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
165 return 1;
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
166 }
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
167 if (phdr->p_type == PT_DYNAMIC)
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
168 break;
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
169 }
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
170 if (i == ehdr->e_phnum)
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
171 {
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
172 fprintf (stderr, "No dynamic section found.\n");
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
173 return 2;
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
174 }
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
175
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
176 if (0 == phdr->p_filesz)
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
177 {
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
178 fprintf (stderr, "Length of dynamic section is zero.\n");
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
179 return 3;
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
180 }
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
181
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
182 return 0;
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
183 }
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
184
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
185 void
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
186 elf_close(int fd)
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
187 {
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
188 close(fd);
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
189 }
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
190
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
191 const char *
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
192 elf_tagname(int tag)
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
193 {
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
194 switch (tag) {
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
195 case DT_RPATH:
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
196 return "RPATH";
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
197 break;
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
198 #if defined(DT_RUNPATH)
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
199 case DT_RUNPATH:
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
200 return "RUNPATH";
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
201 break;
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
202 #endif /* DT_RUNPATH */
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
203 }
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
204 return "UNKNOWN";
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
205 }
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
206
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
207 int
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
208 elf_dynpath_tag(int tag)
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
209 {
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
210 return ( tag == DT_RPATH
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
211 #if defined(DT_RUNPATH)
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
212 || tag == DT_RUNPATH
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
213 #endif /* DT_RUNPATH */
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
214 );
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
215 }

Repositories maintained by Peter Meerwald, pmeerw@pmeerw.net.