Initial commit of vpp code.
[vpp.git] / vppinfra / vppinfra / elf.h
1 /*
2  * Copyright (c) 2015 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 /*
16   Copyright (c) 2001, 2002, 2003 Eliot Dresselhaus
17
18   Permission is hereby granted, free of charge, to any person obtaining
19   a copy of this software and associated documentation files (the
20   "Software"), to deal in the Software without restriction, including
21   without limitation the rights to use, copy, modify, merge, publish,
22   distribute, sublicense, and/or sell copies of the Software, and to
23   permit persons to whom the Software is furnished to do so, subject to
24   the following conditions:
25
26   The above copyright notice and this permission notice shall be
27   included in all copies or substantial portions of the Software.
28
29   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
32   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
33   LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
34   OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
35   WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 */
37
38 #ifndef included_clib_elf_h
39 #define included_clib_elf_h
40
41 #include <vppinfra/format.h>
42 #include <vppinfra/hash.h>
43 #include <vppinfra/vec.h>
44 #include <vppinfra/byte_order.h>
45
46 #define foreach_elf_file_class \
47   _ (CLASS_NONE) _ (32BIT) _ (64BIT)
48
49 #define foreach_elf_data_encoding               \
50   _ (ENCODING_NONE)                             \
51   _ (TWOS_COMPLEMENT_LITTLE_ENDIAN)             \
52   _ (TWOS_COMPLEMENT_BIG_ENDIAN)
53
54 #define ELF_VERSION_NONE (0)
55 #define ELF_VERSION_CURRENT (1)
56
57 #define foreach_elf_abi                         \
58   _ (SYSV, 0)                                   \
59   _ (HPUX, 1)                                   \
60   _ (NETBSD, 2)                                 \
61   _ (LINUX, 3)                                  \
62   _ (SOLARIS, 6)                                \
63   _ (AIX, 7)                                    \
64   _ (IRIX, 8)                                   \
65   _ (FREEBSD, 9)                                \
66   _ (COMPAQ_TRU64, 10)                          \
67   _ (MODESTO, 11)                               \
68   _ (OPENBSD, 12)                               \
69   _ (ARM, 97)                                   \
70   _ (STANDALONE, 255)
71
72 /* Legal values for type (object file type).  */
73 #define foreach_elf_file_type                   \
74   _ (NONE, 0)                                   \
75   _ (RELOC, 1)                                  \
76   _ (EXEC, 2)                                   \
77   _ (SHARED, 3)                                 \
78   _ (CORE, 4)                                   \
79   _ (OS_SPECIFIC_LO, 0xfe00)                    \
80   _ (OS_SPECIFIC_HI, 0xfeff)                    \
81   _ (ARCH_SPECIFIC_LO, 0xff00)                  \
82   _ (ARCH_SPECIFIC_HI, 0xffff)
83
84 /* Legal values for architecture.  */
85 #define foreach_elf_architecture                                        \
86   _ (NONE, 0)                   /* No machine */                        \
87   _ (M32, 1)                    /* AT&T WE 32100 */                     \
88   _ (SPARC, 2)                  /* SUN SPARC */                         \
89   _ (386, 3)                    /* Intel 80386 */                       \
90   _ (68K, 4)                    /* Motorola m68k family */              \
91   _ (88K, 5)                    /* Motorola m88k family */              \
92   _ (860, 7)                    /* Intel 80860 */                       \
93   _ (MIPS, 8)                   /* MIPS R3000 big-endian */             \
94   _ (S370, 9)                   /* IBM System/370 */                    \
95   _ (MIPS_RS3_LE, 10)           /* MIPS R3000 little-endian */          \
96   _ (PARISC, 15)                /* HPPA */                              \
97   _ (VPP500, 17)                /* Fujitsu VPP500 */                    \
98   _ (SPARC32PLUS, 18)           /* Sun's "v8plus" */                    \
99   _ (960, 19)                   /* Intel 80960 */                       \
100   _ (PPC, 20)                   /* PowerPC */                           \
101   _ (PPC64, 21)                 /* PowerPC 64-bit */                    \
102   _ (S390, 22)                  /* IBM S390 */                          \
103   _ (V800, 36)                  /* NEC V800 series */                   \
104   _ (FR20, 37)                  /* Fujitsu FR20 */                      \
105   _ (RH32, 38)                  /* TRW RH-32 */                         \
106   _ (RCE, 39)                   /* Motorola RCE */                      \
107   _ (ARM, 40)                   /* ARM */                               \
108   _ (FAKE_ALPHA, 41)            /* Digital Alpha */                     \
109   _ (SH, 42)                    /* Hitachi SH */                        \
110   _ (SPARCV9, 43)               /* SPARC v9 64-bit */                   \
111   _ (TRICORE, 44)               /* Siemens Tricore */                   \
112   _ (ARC, 45)                   /* Argonaut RISC Core */                \
113   _ (H8_300, 46)                /* Hitachi H8/300 */                    \
114   _ (H8_300H, 47)               /* Hitachi H8/300H */                   \
115   _ (H8S, 48)                   /* Hitachi H8S */                       \
116   _ (H8_500, 49)                /* Hitachi H8/500 */                    \
117   _ (IA_64, 50)                 /* Intel Merced */                      \
118   _ (MIPS_X, 51)                /* Stanford MIPS-X */                   \
119   _ (COLDFIRE, 52)              /* Motorola Coldfire */                 \
120   _ (68HC12, 53)                /* Motorola M68HC12 */                  \
121   _ (MMA, 54)                   /* Fujitsu MMA Multimedia Accel. */     \
122   _ (PCP, 55)                   /* Siemens PCP */                       \
123   _ (NCPU, 56)                  /* Sony nCPU embeeded RISC */           \
124   _ (NDR1, 57)                  /* Denso NDR1 microprocessor */         \
125   _ (STARCORE, 58)              /* Motorola Start*Core processor */     \
126   _ (ME16, 59)                  /* Toyota ME16 processor */             \
127   _ (ST100, 60)                 /* STMicroelectronic ST100  */          \
128   _ (TINYJ, 61)                 /* Advanced Logic Corp. Tinyj */        \
129   _ (X86_64, 62)                /* AMD x86-64 architecture */           \
130   _ (PDSP, 63)                  /* Sony DSP Processor */                \
131   _ (FX66, 66)                  /* Siemens FX66 microcontroller */      \
132   _ (ST9PLUS, 67)               /* STMicroelectronics ST9+ 8/16 mc */   \
133   _ (ST7, 68)                   /* STmicroelectronics ST7 8 bit mc */   \
134   _ (68HC16, 69)                /* Motorola MC68HC16 */                 \
135   _ (68HC11, 70)                /* Motorola MC68HC11 */                 \
136   _ (68HC08, 71)                /* Motorola MC68HC08 */                 \
137   _ (68HC05, 72)                /* Motorola MC68HC05 */                 \
138   _ (SVX, 73)                   /* Silicon Graphics SVx */              \
139   _ (ST19, 74)                  /* STMicroelectronics ST19 8 bit mc */  \
140   _ (VAX, 75)                   /* Digital VAX */                       \
141   _ (CRIS, 76)                  /* Axis 32-bit embedded proc. */        \
142   _ (JAVELIN, 77)               /* Infineon 32-bit embedded proc. */    \
143   _ (FIREPATH, 78)              /* Element 14 64-bit DSP Processor */   \
144   _ (ZSP, 79)                   /* LSI Logic 16-bit DSP Processor */    \
145   _ (MMIX, 80)                  /* Knuth's 64-bit processor */          \
146   _ (HUANY, 81)                 /* Harvard machine-independent */       \
147   _ (PRISM, 82)                 /* SiTera Prism */                      \
148   _ (AVR, 83)                   /* Atmel AVR 8-bit microcontroller */   \
149   _ (FR30, 84)                  /* Fujitsu FR30 */                      \
150   _ (D10V, 85)                  /* Mitsubishi D10V */                   \
151   _ (D30V, 86)                  /* Mitsubishi D30V */                   \
152   _ (V850, 87)                  /* NEC v850 */                          \
153   _ (M32R, 88)                  /* Mitsubishi M32R */                   \
154   _ (MN10300, 89)               /* Matsushita MN10300 */                \
155   _ (MN10200, 90)               /* Matsushita MN10200 */                \
156   _ (PJ, 91)                    /* picoJava */                          \
157   _ (OPENRISC, 92)              /* OpenRISC 32-bit processor */         \
158   _ (ARC_A5, 93)                /* ARC Cores Tangent-A5 */              \
159   _ (XTENSA, 94)                /* Tensilica Xtensa Architecture */     \
160   _ (ALPHA, 0x9026)
161
162 #define _(f) ELF_##f,
163
164 typedef enum {
165   foreach_elf_file_class
166   ELF_N_FILE_CLASS,
167 } elf_file_class_t;
168
169 typedef enum {
170   foreach_elf_data_encoding
171   ELF_N_DATA_ENCODING,
172 } elf_data_encoding_t;
173
174 #undef _
175
176 #define _(f,i) ELF_##f = i,
177
178 typedef enum {
179   foreach_elf_abi
180 } elf_abi_t;
181
182 typedef enum {
183   foreach_elf_file_type
184 } elf_file_type_t;
185
186 #undef _
187
188 typedef enum {
189 #define _(f,i) ELF_ARCH_##f = i,
190   foreach_elf_architecture
191 #undef _
192 } elf_architecture_t;
193
194 typedef struct {
195   /* 0x7f ELF */
196   u8 magic[4];
197
198   elf_file_class_t file_class : 8;
199   elf_data_encoding_t data_encoding : 8;
200   u8 file_version_ident;
201   elf_abi_t abi : 8;
202   u8 abi_version;
203
204   u8 pad[7];
205
206   elf_file_type_t file_type : 16;
207   elf_architecture_t architecture : 16;
208
209   u32 file_version;
210 } elf_first_header_t;
211
212 /* 32/64 bit file header following basic file header. */
213 #define foreach_elf32_file_header               \
214   _ (u32, entry_point)                          \
215   _ (u32, segment_header_file_offset)           \
216   _ (u32, section_header_file_offset)           \
217   _ (u32, flags)                                \
218   _ (u16, n_bytes_this_header)                  \
219   _ (u16, segment_header_size)                  \
220   _ (u16, segment_header_count)                 \
221   _ (u16, section_header_size)                  \
222   _ (u16, section_header_count)                 \
223   _ (u16, section_header_string_table_index)
224
225 #define foreach_elf64_file_header               \
226   _ (u64, entry_point)                          \
227   _ (u64, segment_header_file_offset)           \
228   _ (u64, section_header_file_offset)           \
229   _ (u32, flags)                                \
230   _ (u16, n_bytes_this_header)                  \
231   _ (u16, segment_header_size)                  \
232   _ (u16, segment_header_count)                 \
233   _ (u16, section_header_size)                  \
234   _ (u16, section_header_count)                 \
235   _ (u16, section_header_string_table_index)
236
237 /* Section header.  */
238 #define foreach_elf32_section_header            \
239   _ (u32, name)                                 \
240   _ (u32, type)                                 \
241   _ (u32, flags)                                \
242   _ (u32, exec_address)                         \
243   _ (u32, file_offset)                          \
244   _ (u32, file_size)                            \
245   _ (u32, link)                                 \
246   _ (u32, additional_info)                      \
247   _ (u32, align)                                \
248   _ (u32, entry_size)
249
250 #define foreach_elf64_section_header            \
251   _ (u32, name)                                 \
252   _ (u32, type)                                 \
253   _ (u64, flags)                                \
254   _ (u64, exec_address)                         \
255   _ (u64, file_offset)                          \
256   _ (u64, file_size)                            \
257   _ (u32, link)                                 \
258   _ (u32, additional_info)                      \
259   _ (u64, align)                                \
260   _ (u64, entry_size)
261
262 /* Program segment header.  */
263 #define foreach_elf32_segment_header            \
264   _ (u32, type)                                 \
265   _ (u32, file_offset)                          \
266   _ (u32, virtual_address)                      \
267   _ (u32, physical_address)                     \
268   _ (u32, file_size)                            \
269   _ (u32, memory_size)                          \
270   _ (u32, flags)                                \
271   _ (u32, align)
272
273 #define foreach_elf64_segment_header            \
274   _ (u32, type)                                 \
275   _ (u32, flags)                                \
276   _ (u64, file_offset)                          \
277   _ (u64, virtual_address)                      \
278   _ (u64, physical_address)                     \
279   _ (u64, file_size)                            \
280   _ (u64, memory_size)                          \
281   _ (u64, align)
282
283 /* Symbol table.  */
284 #define foreach_elf32_symbol_header             \
285   _ (u32, name)                                 \
286   _ (u32, value)                                \
287   _ (u32, size)                                 \
288   /* binding upper 4 bits; type lower 4 bits */ \
289   _ (u8, binding_and_type)                      \
290   _ (u8, visibility)                            \
291   _ (u16, section_index)
292
293 #define foreach_elf64_symbol_header             \
294   _ (u32, name)                                 \
295   _ (u8, binding_and_type)                      \
296   _ (u8, visibility)                            \
297   _ (u16, section_index)                        \
298   _ (u64, value)                                \
299   _ (u64, size)
300
301 #define _(t,f) t f;
302
303 typedef struct {
304   foreach_elf32_file_header
305 } elf32_file_header_t;
306
307 typedef struct {
308   foreach_elf64_file_header
309 } elf64_file_header_t;
310
311 typedef struct {
312   foreach_elf32_section_header
313 } elf32_section_header_t;
314
315 typedef struct {
316   foreach_elf64_section_header
317 } elf64_section_header_t;
318
319 typedef struct {
320   foreach_elf32_segment_header
321 } elf32_segment_header_t;
322
323 typedef struct {
324   foreach_elf64_segment_header
325 } elf64_segment_header_t;
326
327 typedef struct {
328   foreach_elf32_symbol_header
329 } elf32_symbol_t;
330
331 typedef struct {
332   foreach_elf64_symbol_header
333 } elf64_symbol_t;
334 #undef _
335
336 /* Special section names.  */
337 #define foreach_elf_symbol_reserved_section_index                       \
338   _ (ABSOLUTE, 0xfff1)          /* Associated symbol is absolute */     \
339   _ (COMMON, 0xfff2)            /* Associated symbol is common */       \
340   _ (XINDEX, 0xffff)            /* Index is in extra table.  */
341
342 #define ELF_SYMBOL_SECTION_RESERVED_LO 0xff00
343 #define ELF_SYMBOL_SECTION_RESERVED_HI 0xffff
344 #define ELF_SYMBOL_SECTION_ARCH_SPECIFIC_LO 0xff00
345 #define ELF_SYMBOL_SECTION_ARCH_SPECIFIC_HI 0xff1f
346 #define ELF_SYMBOL_SECTION_OS_SPECIFIC_LO 0xff20
347 #define ELF_SYMBOL_SECTION_OS_SPECIFIC_HI 0xff3f
348
349 /* Section types. */
350 #define foreach_elf_section_type                                        \
351   _ (UNUSED, 0)                                                         \
352   _ (PROGRAM_DATA, 1)                                                   \
353   _ (SYMBOL_TABLE, 2)                                                   \
354   _ (STRING_TABLE, 3)                                                   \
355   _ (RELOCATION_ADD, 4)                                                 \
356   _ (SYMBOL_TABLE_HASH, 5)                                              \
357   _ (DYNAMIC, 6)                /* Dynamic linking information */       \
358   _ (NOTE, 7)                   /* Notes */                             \
359   _ (NO_BITS, 8)                /* Program space with no data (bss) */  \
360   _ (RELOCATION, 9)             /* Relocation entries, no addends */    \
361   _ (DYNAMIC_SYMBOL_TABLE, 11)  /* Dynamic linker symbol table */       \
362   _ (INIT_ARRAY, 14)            /* Array of constructors */             \
363   _ (FINI_ARRAY, 15)            /* Array of destructors */              \
364   _ (PREINIT_ARRAY, 16)         /* Array of pre-constructors */         \
365   _ (GROUP, 17)                 /* Section group */                     \
366   _ (SYMTAB_SHNDX, 18)          /* Extended section indices */          \
367   _ (OS_SPECIFIC_LO, 0x60000000) /* Start OS-specific */                \
368   _ (GNU_LIBLIST, 0x6ffffff7)   /* Prelink library list */              \
369   _ (CHECKSUM, 0x6ffffff8)      /* Checksum for DSO content.  */        \
370   _ (SUNW_MOVE, 0x6ffffffa)                                             \
371   _ (SUNW_COMDAT, 0x6ffffffb)                                           \
372   _ (SUNW_SYMINFO, 0x6ffffffc)                                          \
373   _ (GNU_VERDEF, 0x6ffffffd)    /* Version definition section.  */      \
374   _ (GNU_VERNEED, 0x6ffffffe) /* Version needs section.  */             \
375   _ (GNU_VERSYM, 0x6fffffff)    /* Version symbol table.  */            \
376   _ (ARCH_SPECIFIC_LO, 0x70000000) /* Start of processor-specific */    \
377   _ (ARCH_SPECIFIC_HI, 0x7fffffff) /* End of processor-specific */      \
378   _ (APP_SPECIFIC_LO, 0x80000000) /* Start of application-specific */   \
379   _ (APP_SPECIFIC_HI, 0x8fffffff) /* End of application-specific */
380
381 /* Section flags. */
382 #define foreach_elf_section_flag                \
383   _ (WRITE, 0)                                  \
384   _ (ALLOC, 1)                                  \
385   _ (EXEC, 2)                                   \
386   _ (MERGE, 3)                                  \
387   _ (STRING_TABLE, 5)                           \
388   _ (INFO_LINK, 6)                              \
389   _ (PRESERVE_LINK_ORDER, 7)                    \
390   _ (OS_NON_CONFORMING, 8)                      \
391   _ (GROUP, 9)                                  \
392   _ (TLS, 10)                                   \
393   _ (OS_SPECIFIC_LO, 20)                        \
394   _ (OS_SPECIFIC_HI, 27)                        \
395   _ (ARCH_SPECIFIC_LO, 28)                      \
396   _ (ARCH_SPECIFIC_HI, 31)
397
398 typedef enum {
399 #define _(f,i) ELF_SECTION_##f = i,
400   foreach_elf_section_type
401 #undef _
402   ELF_SECTION_OS_SPECIFIC_HI = 0x6fffffff,
403 } elf_section_type_t;
404
405 typedef enum {
406 #define _(f,i) ELF_SECTION_FLAG_BIT_##f = i,
407   foreach_elf_section_flag
408 #undef _
409 } elf_section_flag_bit_t;
410
411 typedef enum {
412 #define _(f,i) ELF_SECTION_FLAG_##f = 1 << ELF_SECTION_FLAG_BIT_##f,
413   foreach_elf_section_flag
414 #undef _
415 } elf_section_flag_t;
416
417 /* Symbol bindings (upper 4 bits of binding_and_type). */
418 #define foreach_elf_symbol_binding                                      \
419   _ (LOCAL, 0)                  /* Local symbol */                      \
420   _ (GLOBAL, 1)                 /* Global symbol */                     \
421   _ (WEAK, 2)                   /* Weak symbol */                       \
422   _ (OS_SPECIFIC_LO, 10)        /* Start of OS-specific */              \
423   _ (OS_SPECIFIC_HI, 12)        /* End of OS-specific */                \
424   _ (ARCH_SPECIFIC_LO, 13)      /* Start of processor-specific */       \
425   _ (ARCH_SPECIFIC_HI, 15)      /* End of processor-specific */
426
427 /* Symbol types (lower 4 bits of binding_and_type). */
428 #define foreach_elf_symbol_type                                         \
429   _ (NONE, 0)                                                           \
430   _ (DATA, 1)                   /* Symbol is a data object */           \
431   _ (CODE, 2)                   /* Symbol is a code object */           \
432   _ (SECTION, 3)                /* Symbol associated with a section */  \
433   _ (FILE, 4)                   /* Symbol's name is file name */        \
434   _ (COMMON, 5)                 /* Symbol is a common data object */    \
435   _ (TLS, 6)                    /* Symbol is thread-local data */       \
436   _ (OS_SPECIFIC_LO, 10)        /* Start of OS-specific */              \
437   _ (OS_SPECIFIC_HI, 12)        /* End of OS-specific */                \
438   _ (ARCH_SPECIFIC_LO, 13)      /* Start of processor-specific */       \
439   _ (ARCH_SPECIFIC_HI, 15)      /* End of processor-specific */
440
441 /* Symbol visibility. */
442 #define foreach_elf_symbol_visibility                                   \
443   _ (DEFAULT, 0)                /* Default symbol visibility rules */   \
444   _ (INTERNAL, 1)               /* Processor specific hidden class */   \
445   _ (HIDDEN, 2)                 /* Unavailable in other modules */      \
446   _ (PROTECTED, 3)              /* Not preemptible, not exported */
447
448 /* The syminfo section if available contains additional
449    information about every dynamic symbol.  */
450 typedef struct {
451   u16 bound_to;
452   u16 flags;
453 } elf_symbol_info_t;
454
455 /* Possible values for bound_to.  */
456 #define foreach_elf_symbol_info_bound_to                        \
457   _ (SELF, 0xffff)              /* Symbol bound to self */      \
458   _ (PARENT, 0xfffe)            /* Symbol bound to parent */    \
459   _ (RESERVED_LO, 0xff00)                                       \
460   _ (RESERVED_HI, 0xffff)
461
462 /* Symbol info flags. */
463 #define foreach_elf_symbol_info_flags                                   \
464   _ (DIRECT)                    /* Direct bound symbol */               \
465   _ (PASS_THRU)                 /* Pass-thru symbol for translator */   \
466   _ (COPY)                      /* Symbol is a copy-reloc */            \
467   _ (LAZY_LOAD)                 /* Symbol bound to object to be lazy loaded */
468
469 /* Relocation table entry with/without addend. */
470 typedef struct {
471   u32 address;
472   u32 symbol_and_type;          /* high 24 symbol, low 8 type. */
473   i32 addend[0];
474 } elf32_relocation_t;
475
476 typedef struct {
477   u64 address;
478   u64 symbol_and_type;          /* high 32 symbol, low 32 type. */
479   i64 addend[0];
480 } elf64_relocation_t;
481
482 typedef struct {
483   u64 address;
484   u64 symbol_and_type;
485   u64 addend;
486 } elf_relocation_with_addend_t;
487
488 #define elf_relocation_next(r,type)                                     \
489   ((void *) ((r) + 1)                                                   \
490    + ((type) == ELF_SECTION_RELOCATION_ADD ? sizeof ((r)->addend[0]) : 0))
491
492 /* Segment type. */
493 #define foreach_elf_segment_type                                        \
494   _ (UNUSED, 0)                                                         \
495   _ (LOAD, 1)                   /* Loadable program segment */          \
496   _ (DYNAMIC, 2)                /* Dynamic linking information */       \
497   _ (INTERP, 3)                 /* Program interpreter */               \
498   _ (NOTE, 4)                   /* Auxiliary information */             \
499   _ (SEGMENT_TABLE, 6)          /* Entry for header table itself */     \
500   _ (TLS, 7)                    /* Thread-local storage segment */      \
501   _ (OS_SPECIFIC_LO, 0x60000000) /* Start of OS-specific */             \
502   _ (GNU_EH_FRAME, 0x6474e550)  /* GCC .eh_frame_hdr segment */         \
503   _ (GNU_STACK, 0x6474e551)     /* Indicates stack executability */     \
504   _ (GNU_RELRO, 0x6474e552)     /* Read-only after relocation */        \
505   _ (SUNW_BSS, 0x6ffffffa)      /* Sun specific BSS */                  \
506   _ (SUNW_STACK, 0x6ffffffb)    /* Sun specific stack */                \
507   _ (OS_SPECIFIC_HI, 0x6fffffff) /* End of OS-specific */               \
508   _ (ARCH_SPECIFIC_LO, 0x70000000) /* Start of processor-specific */    \
509   _ (ARCH_SPECIFIC_HI, 0x7fffffff) /* End of processor-specific */
510
511 /* Segment flags. */
512 #define foreach_elf_segment_flag                \
513   _ (EXEC, 0)                                   \
514   _ (WRITE, 1)                                  \
515   _ (READ, 2)                                   \
516   _ (OS_SPECIFIC_LO, 20)                        \
517   _ (OS_SPECIFIC_HI, 27)                        \
518   _ (ARCH_SPECIFIC_LO, 28)                      \
519   _ (ARCH_SPECIFIC_HI, 31)
520
521 typedef enum {
522 #define _(f,i) ELF_SEGMENT_##f = i,
523   foreach_elf_segment_type
524 #undef _
525 } elf_segment_type_t;
526
527 typedef enum {
528 #define _(f,i) ELF_SEGMENT_FLAG_BIT_##f = i,
529   foreach_elf_segment_flag
530 #undef _
531 } elf_segment_flag_bit_t;
532
533 typedef enum {
534 #define _(f,i) ELF_SEGMENT_FLAG_##f = 1 << ELF_SEGMENT_FLAG_BIT_##f,
535   foreach_elf_segment_flag
536 #undef _
537 } elf_segment_flag_t;
538
539 #define foreach_elf32_dynamic_entry_header      \
540   _ (u32, type)                                 \
541   _ (u32, data)
542
543 #define foreach_elf64_dynamic_entry_header      \
544   _ (u64, type)                                 \
545   _ (u64, data)
546   
547 #define _(t,f) t f;
548
549 typedef struct {
550   foreach_elf32_dynamic_entry_header
551 } elf32_dynamic_entry_t;
552
553 typedef struct {
554   foreach_elf64_dynamic_entry_header
555 } elf64_dynamic_entry_t;
556
557 #undef _
558
559 #define foreach_elf_dynamic_entry_type                                  \
560   _ (END, 0)                    /* Marks end of dynamic section */      \
561   _ (NEEDED_LIBRARY, 1)         /* Name of needed library */            \
562   _ (PLT_RELOCATION_SIZE, 2)    /* Size in bytes of PLT relocs */       \
563   _ (PLT_GOT, 3)                /* Processor defined value */           \
564   _ (SYMBOL_HASH, 4)            /* Address of symbol hash table */      \
565   _ (STRING_TABLE, 5)           /* Address of string table */           \
566   _ (SYMBOL_TABLE, 6)           /* Address of symbol table */           \
567   _ (RELA_ADDRESS, 7)           /* Address of Rela relocs */            \
568   _ (RELA_SIZE, 8)              /* Total size of Rela relocs */         \
569   _ (RELA_ENTRY_SIZE, 9)        /* Size of one Rela reloc */            \
570   _ (STRING_TABLE_SIZE, 10)     /* Size of string table */              \
571   _ (SYMBOL_TABLE_ENTRY_SIZE, 11) /* Size of one symbol table entry */  \
572   _ (INIT_FUNCTION, 12)         /* Address of init function */          \
573   _ (FINI_FUNCTION, 13)         /* Address of termination function */   \
574   _ (SONAME, 14)                /* Name of shared object */             \
575   _ (RPATH, 15)                 /* Library search path (deprecated) */  \
576   _ (SYMBOLIC, 16)              /* Start symbol search here */          \
577   _ (REL, 17)                   /* Address of Rel relocs */             \
578   _ (RELSZ, 18)                 /* Total size of Rel relocs */          \
579   _ (RELENT, 19)                /* Size of one Rel reloc */             \
580   _ (PLT_RELOCATION_TYPE, 20)   /* Type of reloc in PLT */              \
581   _ (DEBUG, 21)                 /* For debugging; unspecified */        \
582   _ (TEXTREL, 22)               /* Reloc might modify .text */          \
583   _ (PLT_RELOCATION_ADDRESS, 23) /* Address of PLT relocs */            \
584   _ (BIND_NOW, 24)              /* Process relocations of object */     \
585   _ (INIT_ARRAY, 25)            /* Array with addresses of init fct */  \
586   _ (FINI_ARRAY, 26)            /* Array with addresses of fini fct */  \
587   _ (INIT_ARRAYSZ, 27)          /* Size in bytes of DT_INIT_ARRAY */    \
588   _ (FINI_ARRAYSZ, 28)          /* Size in bytes of DT_FINI_ARRAY */    \
589   _ (RUN_PATH, 29)              /* Library search path */               \
590   _ (FLAGS, 30)                 /* Flags for object being loaded */     \
591   _ (ENCODING, 31)              /* Start of encoded range */            \
592   _ (PREINIT_ARRAY, 32)         /* Array with addresses of fns */       \
593   _ (PREINIT_ARRAY_SIZE, 33)    /* Size of PREINIT_ARRAY in bytes. */   \
594   _ (GNU_PRELINKED, 0x6ffffdf5) /* Prelinking timestamp */              \
595   _ (GNU_CONFLICTSZ, 0x6ffffdf6)        /* Size of conflict section */  \
596   _ (GNU_LIBLISTSZ, 0x6ffffdf7) /* Size of library list */              \
597   _ (CHECKSUM, 0x6ffffdf8)                                              \
598   _ (PLTPADSZ, 0x6ffffdf9)                                              \
599   _ (MOVEENT, 0x6ffffdfa)                                               \
600   _ (MOVESZ, 0x6ffffdfb)                                                \
601   _ (FEATURE_1, 0x6ffffdfc)     /* Feature selection (DTF_*).  */       \
602   _ (POSFLAG_1, 0x6ffffdfd)     /* Flags for following entries.  */     \
603   _ (SYMINSZ, 0x6ffffdfe)       /* Size of syminfo table (in bytes) */  \
604   _ (SYMINENT, 0x6ffffdff)      /* Entry size of syminfo */             \
605   _ (GNU_HASH, 0x6ffffef5)                                              \
606   _ (GNU_CONFLICT, 0x6ffffef8)  /* Start of conflict section */         \
607   _ (GNU_LIBLIST, 0x6ffffef9)   /* Library list */                      \
608   _ (CONFIG, 0x6ffffefa)        /* Configuration information.  */       \
609   _ (DEPAUDIT, 0x6ffffefb)      /* Dependency auditing.  */             \
610   _ (AUDIT, 0x6ffffefc) /* Object auditing.  */                         \
611   _ (PLTPAD, 0x6ffffefd)        /* PLT padding.  */                     \
612   _ (MOVETAB, 0x6ffffefe)       /* Move table.  */                      \
613   _ (SYMINFO, 0x6ffffeff)       /* Syminfo table.  */                   \
614   _ (VERSYM, 0x6ffffff0)                                                \
615   _ (RELACOUNT, 0x6ffffff9)                                             \
616   _ (RELCOUNT, 0x6ffffffa)                                              \
617   _ (FLAGS_1, 0x6ffffffb)       /* State flags, see DF_1_* below.  */   \
618   _ (VERSION_DEF, 0x6ffffffc)   /* Address of version definition table */ \
619   _ (VERSION_DEF_COUNT, 0x6ffffffd)     /* Number of version definitions */ \
620   _ (VERSION_NEED, 0x6ffffffe)  /* Address of table with needed versions */ \
621   _ (VERSION_NEED_COUNT, 0x6fffffff)    /* Number of needed versions */ \
622   _ (AUXILIARY, 0x7ffffffd)      /* Shared object to load before self */ \
623   _ (FILTER, 0x7fffffff)      /* Shared object to get values from */
624
625 typedef enum {
626 #define _(f,n) ELF_DYNAMIC_ENTRY_##f = (n),
627   foreach_elf_dynamic_entry_type
628 #undef _
629 } elf_dynamic_entry_type_t;
630
631 /* Values of `d_un.d_val' in the DT_FLAGS entry.  */
632 #define ELF_DYNAMIC_FLAGS_ORIGIN        (1 << 0) /* Object may use DF_ORIGIN */
633 #define ELF_DYNAMIC_FLAGS_SYMBOLIC      (1 << 1) /* Symbol resolutions starts here */
634 #define ELF_DYNAMIC_FLAGS_TEXT_RELOCATIONS (1 << 2)     /* Object contains text relocations */
635 #define ELF_DYNAMIC_FLAGS_BIND_NOW      (1 << 3)        /* No lazy binding for this object */
636 #define ELF_DYNAMIC_FLAGS_STATIC_TLS    (1 << 4)        /* Module uses the static TLS model */
637
638 /* State flags selectable in the `d_un.d_val' element of the DT_FLAGS_1
639    entry in the dynamic section.  */
640 #define DF_1_NOW        0x00000001      /* Set RTLD_NOW for this object.  */
641 #define DF_1_GLOBAL     0x00000002      /* Set RTLD_GLOBAL for this object.  */
642 #define DF_1_GROUP      0x00000004      /* Set RTLD_GROUP for this object.  */
643 #define DF_1_NODELETE   0x00000008      /* Set RTLD_NODELETE for this object.*/
644 #define DF_1_LOADFLTR   0x00000010      /* Trigger filtee loading at runtime.*/
645 #define DF_1_INITFIRST  0x00000020      /* Set RTLD_INITFIRST for this object*/
646 #define DF_1_NOOPEN     0x00000040      /* Set RTLD_NOOPEN for this object.  */
647 #define DF_1_ORIGIN     0x00000080      /* $ORIGIN must be handled.  */
648 #define DF_1_DIRECT     0x00000100      /* Direct binding enabled.  */
649 #define DF_1_TRANS      0x00000200
650 #define DF_1_INTERPOSE  0x00000400      /* Object is used to interpose.  */
651 #define DF_1_NODEFLIB   0x00000800      /* Ignore default lib search path.  */
652 #define DF_1_NODUMP     0x00001000      /* Object can't be dldump'ed.  */
653 #define DF_1_CONFALT    0x00002000      /* Configuration alternative created.*/
654 #define DF_1_ENDFILTEE  0x00004000      /* Filtee terminates filters search. */
655 #define DF_1_DISPRELDNE 0x00008000      /* Disp reloc applied at build time. */
656 #define DF_1_DISPRELPND 0x00010000      /* Disp reloc applied at run-time.  */
657
658 /* Flags for the feature selection in DT_FEATURE_1.  */
659 #define DTF_1_PARINIT   0x00000001
660 #define DTF_1_CONFEXP   0x00000002
661
662 /* Flags in the DT_POSFLAG_1 entry effecting only the next DT_* entry.  */
663 #define DF_P1_LAZYLOAD  0x00000001      /* Lazyload following object.  */
664 #define DF_P1_GROUPPERM 0x00000002      /* Symbols from next object are not
665                                            generally available.  */
666
667 /* Version definition sections.  */
668 typedef struct
669 {
670   u16 version;
671   u16 flags;
672   u16 index;
673   u16 aux_count;
674   u32 name_hash;
675   u32 aux_byte_offset;
676   u32 byte_offset_next_version_definition;
677 } elf_dynamic_version_definition_t;
678
679 typedef struct {
680   u32 name;
681   u32 next_offset; /* byte offset of ver def aux next entry */
682 } elf_dynamic_version_definition_aux_t;
683
684 /* Version definition flags. */
685 #define ELF_DYNAMIC_VERSION_FILE (1 << 0) /* Version definition of file itself */
686 #define ELF_DYNAMIC_VERSION_WEAK (1 << 1) /* Weak version identifier */
687
688 /* Version symbol index. */
689 #define ELF_DYNAMIC_VERSYM_LOCAL 0      /* Symbol is local.  */
690 #define ELF_DYNAMIC_VERSYM_GLOBAL 1     /* Symbol is global.  */
691 #define ELF_DYNAMIC_VERSYM_RESERVED_LO  0xff00 /* Beginning of reserved entries.  */
692 #define ELF_DYNAMIC_VERSYM_ELIMINATE    0xff01  /* Symbol is to be eliminated.  */
693
694 /* Version dependency section.  */
695 #define foreach_elf_dynamic_version_need_field  \
696   _ (u16, version)                              \
697   _ (u16, aux_count)                            \
698   _ (u32, file_name_offset)                     \
699   _ (u32, first_aux_offset)                     \
700   _ (u32, next_offset)
701
702 #define foreach_elf_dynamic_version_need_aux_field      \
703   _ (u32, hash)                                         \
704   _ (u16, flags)                                        \
705   _ (u16, versym_index)                                 \
706   _ (u32, name)                                         \
707   _ (u32, next_offset)
708
709 typedef struct
710 {
711 #define _(t,f) t f;
712   foreach_elf_dynamic_version_need_field
713 #undef _
714 } elf_dynamic_version_need_t;
715
716 typedef struct
717 {
718 #define _(t,f) t f;
719   foreach_elf_dynamic_version_need_aux_field
720 #undef _
721 } elf_dynamic_version_need_aux_t;
722
723 typedef union
724 {
725   elf_dynamic_version_need_t need;
726   elf_dynamic_version_need_aux_t aux;
727 } elf_dynamic_version_need_union_t;
728
729 /* Note section contents.  Each entry in the note section begins with
730    a header of a fixed form.  */
731
732 typedef struct
733 {
734   u32 name_size;
735   u32 descriptor_size;
736   u32 type;
737 } elf_note_t;
738
739 /* Known names of notes.  */
740
741 /* Solaris entries in the note section have this name.  */
742 #define ELF_NOTE_SOLARIS        "SUNW Solaris"
743
744 /* Note entries for GNU systems have this name.  */
745 #define ELF_NOTE_GNU            "GNU"
746
747
748 /* Defined types of notes for Solaris.  */
749
750 /* Value of descriptor (one word) is desired pagesize for the binary.  */
751 #define ELF_NOTE_PAGESIZE_HINT  1
752
753
754 /* Defined note types for GNU systems.  */
755
756 /* ABI information.  The descriptor consists of words:
757    word 0: OS descriptor
758    word 1: major version of the ABI
759    word 2: minor version of the ABI
760    word 3: subminor version of the ABI
761 */
762 #ifndef ELF_NOTE_ABI
763 #define ELF_NOTE_ABI            1
764 #endif
765
766 /* Known OSes.  These value can appear in word 0 of an ELF_NOTE_ABI
767    note section entry.  */
768 #define ELF_NOTE_OS_LINUX       0
769 #define ELF_NOTE_OS_GNU         1
770 #define ELF_NOTE_OS_SOLARIS2    2
771 #define ELF_NOTE_OS_FREEBSD     3
772
773 /* AMD x86-64 relocations.  */
774 #define foreach_elf_x86_64_relocation_type                              \
775   _ (NONE, 0)                   /* No reloc */                          \
776   _ (DIRECT_64, 1)              /* Direct 64 bit  */                    \
777   _ (PC_REL_I32, 2)             /* PC relative 32 bit signed */         \
778   _ (GOT_REL_32, 3)             /* 32 bit GOT entry */                  \
779   _ (PLT_REL_32, 4)             /* 32 bit PLT address */                \
780   _ (COPY, 5)                   /* Copy symbol at runtime */            \
781   _ (CREATE_GOT, 6)             /* Create GOT entry */                  \
782   _ (CREATE_PLT, 7)             /* Create PLT entry */                  \
783   _ (RELATIVE, 8)               /* Adjust by program base */            \
784   _ (PC_REL_I32_GOT, 9) /* 32 bit PC relative offset to GOT */          \
785   _ (DIRECT_U32, 10)            /* Direct 32 bit zero extended */       \
786   _ (DIRECT_I32, 11)            /* Direct 32 bit sign extended */       \
787   _ (DIRECT_U16, 12)            /* Direct 16 bit zero extended */       \
788   _ (PC_REL_I16, 13)    /* 16 bit sign extended pc relative */          \
789   _ (DIRECT_I8, 14)             /* Direct 8 bit sign extended  */       \
790   _ (PC_REL_I8, 15)     /* 8 bit sign extended pc relative */           \
791   _ (DTPMOD64, 16)              /* ID of module containing symbol */    \
792   _ (DTPOFF64, 17)              /* Offset in module's TLS block */      \
793   _ (TPOFF64, 18)               /* Offset in initial TLS block */       \
794   _ (TLSGD, 19) /* 32 bit signed PC relative offset to two GOT entries for GD symbol */ \
795   _ (TLSLD, 20) /* 32 bit signed PC relative offset to two GOT entries for LD symbol */ \
796   _ (DTPOFF32, 21)              /* Offset in TLS block */               \
797   _ (GOTTPOFF, 22) /* 32 bit signed PC relative offset to GOT entry for IE symbol */ \
798   _ (TPOFF32, 23)               /* Offset in initial TLS, block) */
799
800 typedef struct {
801   elf64_symbol_t * symbols;
802
803   u32 section_index;
804
805   u8 * string_table;
806
807   uword * symbol_by_name;
808 } elf_symbol_table_t;
809
810 always_inline void
811 elf_symbol_table_free (elf_symbol_table_t * s)
812 {
813   vec_free (s->symbols);
814   hash_free (s->symbol_by_name);
815 }
816
817 always_inline u8 *
818 elf_symbol_name (elf_symbol_table_t * t, elf64_symbol_t * sym)
819 { return vec_elt_at_index (t->string_table, sym->name); }
820
821 typedef struct {
822   elf_relocation_with_addend_t * relocations;
823
824   u32 section_index;
825 } elf_relocation_table_t;
826
827 always_inline void
828 elf_relocation_table_free (elf_relocation_table_t * r)
829 {
830   vec_free (r->relocations);
831 }
832
833 typedef struct {
834   elf64_section_header_t header;
835
836   u32 index;
837
838   /* Index of segments containing this section. */
839   uword * segment_index_bitmap;
840
841   /* Aligned size (included padding not included in
842      header.file_size). */
843   u64 align_size;
844
845   i64 exec_address_change;
846
847   u8 * contents;
848 } elf_section_t;
849
850 typedef struct {
851   elf64_segment_header_t header;
852
853   /* Sections contained in this segment. */
854   uword * section_index_bitmap;
855
856   u32 index;
857
858   u8 * contents;
859 } elf_segment_t;
860
861 typedef struct {
862   u8 need_byte_swap;
863
864   u8 parsed_symbols;
865
866   char * file_name;
867
868   elf_first_header_t first_header;
869
870   elf64_file_header_t file_header;
871
872   elf_segment_t * segments;
873
874   elf_section_t * sections;
875
876   uword * section_by_name;
877   uword * section_by_start_address;
878
879   elf_symbol_table_t * symbol_tables;
880   elf_relocation_table_t * relocation_tables;
881
882   char * interpreter;
883
884   elf64_dynamic_entry_t * dynamic_entries;
885   u8 * dynamic_string_table;
886   u32 dynamic_string_table_section_index;
887   u32 dynamic_symbol_table_section_index;
888   u32 dynamic_symbol_table_index;
889   u32 dynamic_section_index;
890   u16 * versym;
891   u32 versym_section_index;
892   elf_dynamic_version_need_union_t * verneed;
893   u32 verneed_section_index;
894 } elf_main_t;
895
896 always_inline void
897 elf_main_init (elf_main_t * em)
898 {
899   memset (em, 0, sizeof (em[0]));
900 }
901
902 always_inline void
903 elf_main_free (elf_main_t * em)
904 {
905   uword i;
906
907   for (i = 0; i < vec_len (em->segments); i++)
908     vec_free (em->segments[i].contents);
909   vec_free (em->segments);
910
911   for (i = 0; i < vec_len (em->sections); i++)
912     vec_free (em->sections[i].contents);
913   vec_free (em->sections);
914
915   hash_free (em->section_by_name);
916   for (i = 0; i < vec_len (em->symbol_tables); i++)
917     elf_symbol_table_free (em->symbol_tables + i);
918   for (i = 0; i < vec_len (em->relocation_tables); i++)
919     elf_relocation_table_free (em->relocation_tables + i);
920
921   vec_free (em->dynamic_entries);
922   vec_free (em->interpreter);
923 }
924
925 always_inline void
926 elf_get_segment_contents (elf_main_t * em,
927                           void * data,
928                           uword segment_index)
929 {
930   elf_segment_t * g = vec_elt_at_index (em->segments, segment_index);
931   if (! g->contents)
932     vec_add (g->contents, data + g->header.file_offset, g->header.memory_size);
933 }
934
935 always_inline void *
936 elf_get_section_contents (elf_main_t * em,
937                           uword section_index,
938                           uword elt_size)
939 {
940   elf_section_t * s;
941   void * result;
942
943   s = vec_elt_at_index (em->sections, section_index);
944
945   result = 0;
946   if (vec_len (s->contents) > 0)
947     {
948       /* Make vector copy of contents with given element size. */
949       result = _vec_resize (result,
950                             vec_len (s->contents) / elt_size,
951                             vec_len (s->contents),
952                             /* header_bytes */ 0,
953                             /* align */ 0);
954       memcpy (result, s->contents, vec_len (s->contents));
955     }
956
957   return result;
958 }
959
960 always_inline void
961 elf_set_section_contents (elf_main_t * em,
962                           uword section_index,
963                           void * new_contents,
964                           uword n_content_bytes)
965 {
966   elf_section_t * s;
967
968   s = vec_elt_at_index (em->sections, section_index);
969   vec_free (s->contents);
970   vec_add (s->contents, new_contents, n_content_bytes);
971 }
972
973 always_inline u8 *
974 elf_section_name (elf_main_t * em, elf_section_t * s)
975 {
976   elf_section_t * es = vec_elt_at_index (em->sections, em->file_header.section_header_string_table_index);
977   return vec_elt_at_index (es->contents, s->header.name);
978 }
979
980 always_inline u8
981 elf_swap_u8 (elf_main_t * em, u8 x)
982 { return x; }
983
984 always_inline u16
985 elf_swap_u16 (elf_main_t * em, u16 x)
986 { return em->need_byte_swap ? clib_byte_swap_u16 (x) : x; }
987
988 always_inline u32
989 elf_swap_u32 (elf_main_t * em, u32 x)
990 { return em->need_byte_swap ? clib_byte_swap_u32 (x) : x; }
991
992 always_inline u64
993 elf_swap_u64 (elf_main_t * em, u64 x)
994 { return em->need_byte_swap ? clib_byte_swap_u64 (x) : x; }
995
996 #define FORMAT_ELF_MAIN_SYMBOLS (1 << 0)
997 #define FORMAT_ELF_MAIN_RELOCATIONS (1 << 1)
998 #define FORMAT_ELF_MAIN_DYNAMIC (1 << 2)
999
1000 format_function_t format_elf_main;
1001 format_function_t format_elf_symbol;
1002
1003 clib_error_t * elf_read_file (elf_main_t * em, char * file_name);
1004 clib_error_t * elf_write_file (elf_main_t * em, char * file_name);
1005 clib_error_t * elf_delete_named_section (elf_main_t * em, char * section_name);
1006 clib_error_t * elf_parse (elf_main_t * em, void * data, uword data_bytes);
1007 void elf_parse_symbols (elf_main_t * em);
1008
1009 clib_error_t *
1010 elf_get_section_by_name (elf_main_t * em, char * section_name, elf_section_t ** result);
1011 clib_error_t *
1012 elf_get_section_by_start_address (elf_main_t * em, uword start_address, elf_section_t ** result);
1013
1014 void
1015 elf_create_section_with_contents (elf_main_t * em,
1016                                   char * section_name,
1017                                   elf64_section_header_t * header,
1018                                   void * contents,
1019                                   uword n_content_bytes);
1020 uword elf_delete_segment_with_type (elf_main_t * em, elf_segment_type_t segment_type);
1021 void elf_set_dynamic_entries (elf_main_t * em);
1022
1023 #endif /* included_clib_elf_h */