GPLv2 release
[centaur.git] / include / libelfu / types.h
1 /* This file is part of centaur.
2  *
3  * centaur is free software: you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License 2 as
5  * published by the Free Software Foundation.
6
7  * centaur is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11
12  * You should have received a copy of the GNU General Public License
13  * along with centaur.  If not, see <http://www.gnu.org/licenses/>.
14  */
15
16 /*!
17  * @file types.h
18  * @brief libelfu's Elfu* types.
19  *
20  * libelfu provides an abstraction of the data in ELF files
21  * to more closely model the dependencies between them, and
22  * thus to facilitate consistent editing.
23  */
24
25 #ifndef __LIBELFU_TYPES_H__
26 #define __LIBELFU_TYPES_H__
27
28 #include <sys/queue.h>
29
30 #include <elf.h>
31 #include <gelf.h>
32
33
34 /*!
35  * ELFU model of a symbol table entry.
36  */
37 typedef struct ElfuSym {
38   /*! Offset into linked string table */
39   GElf_Word name;
40
41
42   /*! Explicit value */
43   GElf_Addr value;
44
45   /*! Size of object referenced */
46   GElf_Word size;
47
48
49   /*! Binding type (GLOBAL, LOCAL, ...) */
50   unsigned char bind;
51
52   /*! Type (FUNC, SECTION, ...) */
53   unsigned char type;
54
55   /*! Always 0, currently meaningless (see ELF spec) */
56   unsigned char other;
57
58
59   /*! If non-null, the section referenced. */
60   struct ElfuScn *scnptr;
61
62   /*! If scnptr is invalid, then this is ABS, COMMON, or UNDEF. */
63   int shndx;
64
65
66   /*! Entry in a symbol table */
67   CIRCLEQ_ENTRY(ElfuSym) elem;
68 } ElfuSym;
69
70
71
72 /*!
73  * ELFU model of a symbol table.
74  */
75 typedef struct ElfuSymtab {
76   /*! All parsed symbols */
77   CIRCLEQ_HEAD(Syms, ElfuSym) syms;
78 } ElfuSymtab;
79
80
81
82
83
84 /*!
85  * ELFU model of a relocation entry.
86  */
87 typedef struct ElfuRel {
88   /*! Location to patch */
89   GElf_Addr offset;
90
91
92   /*! Symbol to calculate value for */
93   GElf_Word sym;
94
95   /*! Relocation type (machine specific) */
96   unsigned char type;
97
98
99   /*! addendUsed == 0: Use addend from relocation destination (REL).
100    *  addendUsed != 0: Use explicit addend (RELA). */
101   int addendUsed;
102
103   /*! Explicit addend in RELA tables */
104   GElf_Sword addend;
105
106
107   /*! Element in table */
108   CIRCLEQ_ENTRY(ElfuRel) elem;
109 } ElfuRel;
110
111
112
113 /*!
114  * ELFU model of a relocation table.
115  */
116 typedef struct ElfuReltab {
117   /*! All parsed relocation entries */
118   CIRCLEQ_HEAD(Rels, ElfuRel) rels;
119 } ElfuReltab;
120
121
122
123
124
125
126
127 /*!
128  * ELFU model of an ELF section.
129  *
130  * Several members of the SHDR are repeated in abstract form and take
131  * precedence if present. For example, if linkptr is non-NULL then
132  * it points to another section and the SHDR's numeric sh_link member
133  * is to be ignored and recomputed by the layouter.
134  */
135 typedef struct ElfuScn {
136   /*! SHDR as per ELF specification.
137    *  Several values are ignored or recomputed from other members of
138    *  ElfuScn. */
139   GElf_Shdr shdr;
140
141   /*! malloc()ed buffer containing this section's flat data backbuffer.
142    *  Present for all sections that occupy space in the file. */
143   char *databuf;
144
145
146   /*! Pointer to a section, meaning dependent on sh_type.
147    *  Preferred over sh_link if non-null. */
148   struct ElfuScn *linkptr;
149
150   /*! Pointer to a section, meaning dependent on sh_type.
151    *  Preferred over sh_info if non-null. */
152   struct ElfuScn *infoptr;
153
154
155   /*! If this section is a clone of another section, then this
156    *  points to that other section.
157    *  Used during code injection to facilitate relocation. */
158   struct ElfuScn *oldptr;
159
160
161   /*! Translated contents of a symbol table. */
162   struct ElfuSymtab symtab;
163
164   /*! Translated contents of a relocation table. */
165   struct ElfuReltab reltab;
166
167
168   /*! Element linking this section into an ElfuPhdr's childScnList,
169    *  or into an ElfuElf's orphanScnList. */
170   CIRCLEQ_ENTRY(ElfuScn) elemChildScn;
171 } ElfuScn;
172
173
174
175 /*!
176  * ELFU model of an ELF PHDR.
177  */
178 typedef struct ElfuPhdr {
179   /*! PHDR as per ELF specification. */
180   GElf_Phdr phdr;
181
182
183   /*! Sections to be shifted in file/memory together with this PHDR */
184   CIRCLEQ_HEAD(ChildScnList, ElfuScn) childScnList;
185
186   /*! PHDRs to be shifted in file/memory together with this PHDR */
187   CIRCLEQ_HEAD(ChildPhdrList, ElfuPhdr) childPhdrList;
188
189
190   /*! Element linking this section into an ElfuPhdr's childPhdrList */
191   CIRCLEQ_ENTRY(ElfuPhdr) elemChildPhdr;
192
193   /*! Element linking this section into an ElfuElf's phdrList */
194   CIRCLEQ_ENTRY(ElfuPhdr) elem;
195 } ElfuPhdr;
196
197
198
199 /*!
200  * ELFU model of an ELF file header
201  */
202 typedef struct {
203   /*! The ELF class -- ELFCLASS32/ELFCLASS64 */
204   int elfclass;
205
206   /*! EHDR as per ELF specification */
207   GElf_Ehdr ehdr;
208
209
210   /*! PHDRs that are not dependent on others.
211    *  Usually, LOAD and GNU_STACK. */
212   CIRCLEQ_HEAD(PhdrList, ElfuPhdr) phdrList;
213
214   /*! Sections not in any PHDR, and thus not loaded at runtime.
215    *  Usually .symtab, .strtab, .shstrtab, .debug_*, ... */
216   CIRCLEQ_HEAD(OrphanScnList, ElfuScn) orphanScnList;
217
218
219   /*! Pointer to ElfuScn containing .shstrtab.
220    *  Parsed from EHDR for convenience. */
221   ElfuScn *shstrtab;
222
223   /*! Pointer to ElfuScn containing .symtab.
224    *  Parsed from list of sections for convenience. */
225   ElfuScn *symtab;
226 } ElfuElf;
227
228 #endif