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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
15 #include <vppinfra/bitmap.h>
16 #include <vppinfra/byte_order.h>
17 #include <vppinfra/error.h>
18 #include <vppinfra/hash.h>
19 #include <vppinfra/vec.h>
20 #include <vppinfra/elf.h>
23 elf_swap_first_header (elf_main_t * em, elf_first_header_t * h)
25 h->architecture = elf_swap_u16 (em, h->architecture);
26 h->file_type = elf_swap_u16 (em, h->file_type);
27 h->file_version = elf_swap_u32 (em, h->file_version);
31 elf_swap_verneed (elf_dynamic_version_need_t * n)
33 #define _(t,f) n->f = clib_byte_swap_##t (n->f);
34 foreach_elf_dynamic_version_need_field
39 elf_swap_verneed_aux (elf_dynamic_version_need_aux_t * n)
41 #define _(t,f) n->f = clib_byte_swap_##t (n->f);
42 foreach_elf_dynamic_version_need_aux_field
47 elf_get_section_by_name (elf_main_t * em, char * section_name, elf_section_t ** result)
51 p = hash_get_mem (em->section_by_name, section_name);
53 return clib_error_return (0, "no such section `%s'", section_name);
55 *result = vec_elt_at_index (em->sections, p[0]);
60 elf_get_section_by_start_address_no_check (elf_main_t * em, uword start_address)
62 uword * p = hash_get (em->section_by_start_address, start_address);
63 return p ? vec_elt_at_index (em->sections, p[0]) : 0;
67 elf_get_section_by_start_address (elf_main_t * em, uword start_address, elf_section_t ** result)
69 elf_section_t * s = elf_get_section_by_start_address_no_check (em, start_address);
71 return clib_error_return (0, "no section with address 0x%wx", start_address);
77 format_elf_section_type (u8 * s, va_list * args)
79 elf_section_type_t type = va_arg (*args, elf_section_type_t);
84 #define _(f,i) case ELF_SECTION_##f: t = #f; break;
85 foreach_elf_section_type
90 s = format (s, "unknown 0x%x", type);
92 s = format (s, "%s", t);
97 format_elf_section (u8 * s, va_list * args)
99 elf_main_t * em = va_arg (*args, elf_main_t *);
100 elf_section_t * es = va_arg (*args, elf_section_t *);
101 elf64_section_header_t * h = &es->header;
104 return format (s, "%=40s%=10s%=20s%=8s%=16s%=16s%=16s",
105 "Name", "Index", "Type", "Size", "Align", "Address", "File offset");
107 s = format (s, "%-40s%10d%=20U%8Lx%16d%16Lx %Lx-%Lx",
108 elf_section_name (em, es),
110 format_elf_section_type, h->type,
114 h->file_offset, h->file_offset + h->file_size);
119 if (h->flags & ELF_SECTION_FLAG_##f) s = format (s, " %s", #f);
120 foreach_elf_section_flag;
128 format_elf_segment_type (u8 * s, va_list * args)
130 elf_segment_type_t type = va_arg (*args, elf_segment_type_t);
135 #define _(f,i) case ELF_SEGMENT_##f: t = #f; break;
136 foreach_elf_segment_type
141 s = format (s, "unknown 0x%x", type);
143 s = format (s, "%s", t);
148 format_elf_segment (u8 * s, va_list * args)
150 elf_segment_t * es = va_arg (*args, elf_segment_t *);
151 elf64_segment_header_t * h = &es->header;
154 return format (s, "%=16s%=16s%=16s%=16s",
155 "Type", "Virt. Address", "Phys. Address", "Size");
157 s = format (s, "%=16U%16Lx%16Lx%16Lx%16Lx",
158 format_elf_segment_type, h->type,
167 if (h->flags & ELF_SEGMENT_FLAG_##f) s = format (s, " %s", #f);
168 foreach_elf_segment_flag;
176 format_elf_symbol_binding_and_type (u8 * s, va_list * args)
178 int bt = va_arg (*args, int);
180 char * type_string = 0;
181 char * binding_string = 0;
183 switch ((b = ((bt >> 4) & 0xf)))
185 #define _(f,n) case n: binding_string = #f; break;
186 foreach_elf_symbol_binding;
191 switch ((t = ((bt >> 0) & 0xf)))
193 #define _(f,n) case n: type_string = #f; break;
194 foreach_elf_symbol_type;
200 s = format (s, "%s", binding_string);
202 s = format (s, "binding 0x%x", b);
205 s = format (s, " %s", type_string);
207 s = format (s, " type 0x%x", t);
213 format_elf_symbol_visibility (u8 * s, va_list * args)
215 int visibility = va_arg (*args, int);
220 #define _(f,n) case n: t = #f; break;
221 foreach_elf_symbol_visibility
226 return format (s, "%s", t);
228 return format (s, "unknown 0x%x", visibility);
232 format_elf_symbol_section_name (u8 * s, va_list * args)
234 elf_main_t * em = va_arg (*args, elf_main_t *);
235 int si = va_arg (*args, int);
238 if (si < vec_len (em->sections))
240 elf_section_t * es = vec_elt_at_index (em->sections, si);
241 return format (s, "%s", elf_section_name (em, es));
244 if (si >= ELF_SYMBOL_SECTION_RESERVED_LO
245 && si <= ELF_SYMBOL_SECTION_RESERVED_HI)
249 #define _(f,n) case n: t = #f; break;
250 foreach_elf_symbol_reserved_section_index
258 return format (s, "%s", t);
260 return format (s, "unknown 0x%x", si);
263 u8 * format_elf_symbol (u8 * s, va_list * args)
265 elf_main_t * em = va_arg (*args, elf_main_t *);
266 elf_symbol_table_t * t = va_arg (*args, elf_symbol_table_t *);
267 elf64_symbol_t * sym = va_arg (*args, elf64_symbol_t *);
270 return format (s, "%=32s%=16s%=16s%=16s%=16s%=16s",
271 "Symbol", "Size", "Value", "Type", "Visibility", "Section");
273 s = format (s, "%-32s%16Ld%16Lx%=16U%=16U%U",
274 elf_symbol_name (t, sym),
275 sym->size, sym->value,
276 format_elf_symbol_binding_and_type, sym->binding_and_type,
277 format_elf_symbol_visibility, sym->visibility,
278 format_elf_symbol_section_name, em, sym->section_index);
284 format_elf_relocation_type (u8 * s, va_list * args)
286 elf_main_t * em = va_arg (*args, elf_main_t *);
287 int type = va_arg (*args, int);
290 switch (em->first_header.architecture)
292 #define _(f,i) [i] = #f,
294 case ELF_ARCH_X86_64:
296 static char * tab[] = {
297 foreach_elf_x86_64_relocation_type
301 if (type < ARRAY_LEN (tab))
311 s = format (s, "0x%02x", type);
313 s = format (s, "%s", t);
319 format_elf_relocation (u8 * s, va_list * args)
321 elf_main_t * em = va_arg (*args, elf_main_t *);
322 elf_relocation_with_addend_t * r = va_arg (*args, elf_relocation_with_addend_t *);
323 elf_symbol_table_t * t;
324 elf64_symbol_t * sym;
327 return format (s, "%=16s%=16s%=16s", "Address", "Type", "Symbol");
329 t = vec_elt_at_index (em->symbol_tables, 0);
330 sym = vec_elt_at_index (t->symbols, r->symbol_and_type >> 32);
332 s = format (s, "%16Lx%16U",
334 format_elf_relocation_type, em, r->symbol_and_type & 0xff);
336 if (sym->section_index != 0)
339 es = vec_elt_at_index (em->sections, sym->section_index);
340 s = format (s, " (section %s)", elf_section_name (em, es));
344 s = format (s, " %s", elf_symbol_name (t, sym));
349 s = format (s, " %c 0x%Lx",
358 format_elf_dynamic_entry_type (u8 * s, va_list * args)
360 u32 type = va_arg (*args, u32);
364 #define _(f,n) case n: t = #f; break;
365 foreach_elf_dynamic_entry_type;
370 return format (s, "%s", t);
372 return format (s, "unknown 0x%x", type);
376 format_elf_dynamic_entry (u8 * s, va_list * args)
378 elf_main_t * em = va_arg (*args, elf_main_t *);
379 elf64_dynamic_entry_t * e = va_arg (*args, elf64_dynamic_entry_t *);
382 return format (s, "%=40s%=16s", "Type", "Data");
384 s = format (s, "%=40U",
385 format_elf_dynamic_entry_type, (u32) e->type);
388 case ELF_DYNAMIC_ENTRY_NEEDED_LIBRARY:
389 case ELF_DYNAMIC_ENTRY_RPATH:
390 case ELF_DYNAMIC_ENTRY_RUN_PATH:
391 s = format (s, "%s", em->dynamic_string_table + e->data);
394 case ELF_DYNAMIC_ENTRY_INIT_FUNCTION:
395 case ELF_DYNAMIC_ENTRY_FINI_FUNCTION:
396 case ELF_DYNAMIC_ENTRY_SYMBOL_HASH:
397 case ELF_DYNAMIC_ENTRY_GNU_HASH:
398 case ELF_DYNAMIC_ENTRY_STRING_TABLE:
399 case ELF_DYNAMIC_ENTRY_SYMBOL_TABLE:
400 case ELF_DYNAMIC_ENTRY_PLT_GOT:
401 case ELF_DYNAMIC_ENTRY_PLT_RELOCATION_ADDRESS:
402 case ELF_DYNAMIC_ENTRY_RELA_ADDRESS:
403 case ELF_DYNAMIC_ENTRY_VERSION_NEED:
404 case ELF_DYNAMIC_ENTRY_VERSYM:
406 elf_section_t * es = elf_get_section_by_start_address_no_check (em, e->data);
408 s = format (s, "section %s", elf_section_name (em, es));
410 s = format (s, "0x%Lx", e->data);
415 s = format (s, "0x%Lx", e->data);
422 static u8 * format_elf_architecture (u8 * s, va_list * args)
424 int a = va_arg (*args, int);
429 #define _(f,n) case n: t = #f; break;
430 foreach_elf_architecture;
433 return format (s, "unknown 0x%x", a);
436 return format (s, "%s", t);
439 static u8 * format_elf_abi (u8 * s, va_list * args)
441 int a = va_arg (*args, int);
446 #define _(f,n) case n: t = #f; break;
450 return format (s, "unknown 0x%x", a);
453 return format (s, "%s", t);
456 static u8 * format_elf_file_class (u8 * s, va_list * args)
458 int a = va_arg (*args, int);
463 #define _(f) case ELF_##f: t = #f; break;
464 foreach_elf_file_class;
467 return format (s, "unknown 0x%x", a);
470 return format (s, "%s", t);
473 static u8 * format_elf_file_type (u8 * s, va_list * args)
475 int a = va_arg (*args, int);
478 if (a >= ELF_ARCH_SPECIFIC_LO && a <= ELF_ARCH_SPECIFIC_HI)
479 return format (s, "arch-specific 0x%x", a - ELF_ARCH_SPECIFIC_LO);
481 if (a >= ELF_OS_SPECIFIC_LO && a <= ELF_OS_SPECIFIC_HI)
482 return format (s, "os-specific 0x%x", a - ELF_OS_SPECIFIC_LO);
486 #define _(f,n) case n: t = #f; break;
487 foreach_elf_file_type;
490 return format (s, "unknown 0x%x", a);
493 return format (s, "%s", t);
496 static u8 * format_elf_data_encoding (u8 * s, va_list * args)
498 int a = va_arg (*args, int);
503 #define _(f) case ELF_##f: t = #f; break;
504 foreach_elf_data_encoding;
507 return format (s, "unknown 0x%x", a);
510 return format (s, "%s", t);
513 static int elf_section_offset_compare (void *a1, void *a2)
515 elf_section_t *s1 = a1;
516 elf_section_t *s2 = a2;
518 return ((i64)s1->header.file_offset - (i64)s2->header.file_offset);
521 static int elf_segment_va_compare (void *a1, void *a2)
523 elf_segment_t *s1 = a1;
524 elf_segment_t *s2 = a2;
526 return ((i64)s1->header.virtual_address - (i64)s2->header.virtual_address);
530 format_elf_main (u8 * s, va_list * args)
532 elf_main_t * em = va_arg (*args, elf_main_t *);
533 u32 verbose = va_arg (*args, u32);
534 elf64_file_header_t * fh = &em->file_header;
536 s = format (s, "File header: machine: %U, file type/class %U/%U, data-encoding: %U, abi: %U version %d\n",
537 format_elf_architecture, em->first_header.architecture,
538 format_elf_file_type, em->first_header.file_type,
539 format_elf_file_class, em->first_header.file_class,
540 format_elf_data_encoding, em->first_header.data_encoding,
541 format_elf_abi, em->first_header.abi, em->first_header.abi_version);
543 s = format (s, " entry 0x%Lx, arch-flags 0x%x",
544 em->file_header.entry_point, em->file_header.flags);
547 s = format (s, "\n interpreter: %s", em->interpreter);
550 elf_section_t * h, * copy;
553 vec_foreach (h, em->sections)
554 if (h->header.type != ~0)
555 vec_add1 (copy, h[0]);
557 vec_sort_with_function (copy, elf_section_offset_compare);
559 s = format (s, "\nSections %d at file offset 0x%Lx-0x%Lx:\n",
560 fh->section_header_count,
561 fh->section_header_file_offset,
562 fh->section_header_file_offset + fh->section_header_count * fh->section_header_size);
563 s = format (s, "%U\n", format_elf_section, em, 0);
564 vec_foreach (h, copy)
565 s = format (s, "%U\n", format_elf_section, em, h);
571 elf_segment_t * h, * copy;
574 vec_foreach (h, em->segments)
575 if (h->header.type != ELF_SEGMENT_UNUSED && h->header.type != ~0)
576 vec_add1 (copy, h[0]);
578 /* Sort segments by address. */
579 vec_sort_with_function (copy, elf_segment_va_compare);
581 s = format (s, "\nSegments: %d at file offset 0x%Lx-0x%Lx:\n",
582 fh->segment_header_count,
583 fh->segment_header_file_offset,
584 fh->segment_header_file_offset + fh->segment_header_count * fh->segment_header_size);
586 s = format (s, "%U\n", format_elf_segment, 0);
587 vec_foreach (h, copy)
588 s = format (s, "%U\n", format_elf_segment, h);
593 if ((verbose & FORMAT_ELF_MAIN_SYMBOLS)
594 && vec_len (em->symbol_tables) > 0)
596 elf_symbol_table_t * t;
597 elf64_symbol_t * sym;
600 vec_foreach (t, em->symbol_tables)
602 es = vec_elt_at_index (em->sections, t->section_index);
603 s = format (s, "\nSymbols for section %s:\n", elf_section_name (em, es));
605 s = format (s, "%U\n", format_elf_symbol, em, 0, 0);
606 vec_foreach (sym, t->symbols)
607 s = format (s, "%U\n", format_elf_symbol, em, t, sym);
611 if ((verbose & FORMAT_ELF_MAIN_RELOCATIONS)
612 && vec_len (em->relocation_tables) > 0)
614 elf_relocation_table_t * t;
615 elf_relocation_with_addend_t * r;
618 vec_foreach (t, em->relocation_tables)
620 es = vec_elt_at_index (em->sections, t->section_index);
622 s = format (s, "\nRelocations for section %s:\n",
623 elf_section_name (em, es));
625 s = format (s, "%U\n", format_elf_relocation, em, 0);
626 vec_foreach (r, t->relocations)
628 s = format (s, "%U\n",
629 format_elf_relocation, em, r);
634 if ((verbose & FORMAT_ELF_MAIN_DYNAMIC)
635 && vec_len (em->dynamic_entries) > 0)
637 elf64_dynamic_entry_t * es, * e;
638 s = format (s, "\nDynamic linker information:\n");
639 es = vec_dup (em->dynamic_entries);
640 s = format (s, "%U\n", format_elf_dynamic_entry, em, 0);
642 s = format (s, "%U\n", format_elf_dynamic_entry, em, e);
649 elf_parse_segments (elf_main_t * em, void * data)
651 void * d = data + em->file_header.segment_header_file_offset;
652 uword n = em->file_header.segment_header_count;
655 vec_resize (em->segments, n);
657 for (i = 0; i < n; i++)
659 em->segments[i].index = i;
661 if (em->first_header.file_class == ELF_64BIT)
663 elf64_segment_header_t * h = d;
664 #define _(t,f) em->segments[i].header.f = elf_swap_##t (em, h->f);
665 foreach_elf64_segment_header
671 elf32_segment_header_t * h = d;
672 #define _(t,f) em->segments[i].header.f = elf_swap_##t (em, h->f);
673 foreach_elf32_segment_header
681 elf_parse_sections (elf_main_t * em, void * data)
683 elf64_file_header_t * fh = &em->file_header;
685 void * d = data + fh->section_header_file_offset;
686 uword n = fh->section_header_count;
689 vec_resize (em->sections, n);
691 for (i = 0; i < n; i++)
693 s = em->sections + i;
697 if (em->first_header.file_class == ELF_64BIT)
699 elf64_section_header_t * h = d;
700 #define _(t,f) em->sections[i].header.f = elf_swap_##t (em, h->f);
701 foreach_elf64_section_header
707 elf32_section_header_t * h = d;
708 #define _(t,f) em->sections[i].header.f = elf_swap_##t (em, h->f);
709 foreach_elf32_section_header
714 if (s->header.type != ELF_SECTION_NO_BITS)
715 vec_add (s->contents, data + s->header.file_offset, s->header.file_size);
718 s = vec_elt_at_index (em->sections, fh->section_header_string_table_index);
721 = hash_create_string (/* # elts */ vec_len (em->sections),
722 /* sizeof of value */ sizeof (uword));
724 vec_foreach (s, em->sections)
726 hash_set_mem (em->section_by_name,
727 elf_section_name (em, s),
729 hash_set (em->section_by_start_address,
730 s->header.exec_address,
736 add_symbol_table (elf_main_t * em, elf_section_t * s)
738 elf_symbol_table_t * tab;
739 elf32_symbol_t * sym32;
740 elf64_symbol_t * sym64;
743 if (s->header.type == ELF_SECTION_DYNAMIC_SYMBOL_TABLE)
744 em->dynamic_symbol_table_index = vec_len (em->symbol_tables);
746 vec_add2 (em->symbol_tables, tab, 1);
748 tab->section_index = s->index;
750 if (em->first_header.file_class == ELF_64BIT)
752 tab->symbols = elf_get_section_contents (em, s - em->sections, sizeof (tab->symbols[0]));
753 for (i = 0; i < vec_len (tab->symbols); i++)
755 #define _(t,f) tab->symbols[i].f = elf_swap_##t (em, tab->symbols[i].f);
756 foreach_elf64_symbol_header;
762 sym32 = elf_get_section_contents (em, s - em->sections, sizeof (sym32[0]));
763 vec_clone (tab->symbols, sym32);
764 for (i = 0; i < vec_len (tab->symbols); i++)
766 #define _(t,f) tab->symbols[i].f = elf_swap_##t (em, sym32[i].f);
767 foreach_elf32_symbol_header;
772 if (s->header.link == 0)
776 elf_get_section_contents (em, s->header.link, sizeof (tab->string_table[0]));
778 = hash_create_string (/* # elts */ vec_len (tab->symbols),
779 /* sizeof of value */ sizeof (uword));
781 vec_foreach (sym64, tab->symbols)
783 if (sym64->name != 0)
784 hash_set_mem (tab->symbol_by_name,
785 tab->string_table + sym64->name,
786 sym64 - tab->symbols);
791 add_relocation_table (elf_main_t * em, elf_section_t * s)
793 uword has_addend = s->header.type == ELF_SECTION_RELOCATION_ADD;
794 elf_relocation_table_t * t;
797 vec_add2 (em->relocation_tables, t, 1);
798 t->section_index = s - em->sections;
800 if (em->first_header.file_class == ELF_64BIT)
802 elf64_relocation_t * r, * rs;
804 rs = elf_get_section_contents (em, t->section_index,
805 sizeof (rs[0]) + has_addend * sizeof (rs->addend[0]));
807 if (em->need_byte_swap)
810 for (i = 0; i < vec_len (r); i++)
812 r->address = elf_swap_u64 (em, r->address);
813 r->symbol_and_type = elf_swap_u32 (em, r->symbol_and_type);
815 r->addend[0] = elf_swap_u64 (em, r->addend[0]);
816 r = elf_relocation_next (r, s->header.type);
820 vec_resize (t->relocations, vec_len (rs));
821 memcpy (t->relocations, rs, vec_bytes (t->relocations));
826 elf_relocation_with_addend_t * r;
827 elf32_relocation_t * r32, * r32s;
829 r32s = elf_get_section_contents (em, t->section_index,
830 sizeof (r32s[0]) + has_addend * sizeof (r32s->addend[0]));
831 vec_resize (t->relocations, vec_len (r32s));
834 vec_foreach (r, t->relocations)
836 r->address = elf_swap_u32 (em, r32->address);
837 r->symbol_and_type = elf_swap_u32 (em, r->symbol_and_type);
838 r->addend = has_addend ? elf_swap_u32 (em, r32->addend[0]) : 0;
839 r32 = elf_relocation_next (r32, s->header.type);
846 void elf_parse_symbols (elf_main_t * em)
850 /* No need to parse symbols twice. */
851 if (em->parsed_symbols)
853 em->parsed_symbols = 1;
855 vec_foreach (s, em->sections)
857 switch (s->header.type)
859 case ELF_SECTION_SYMBOL_TABLE:
860 case ELF_SECTION_DYNAMIC_SYMBOL_TABLE:
861 add_symbol_table (em, s);
864 case ELF_SECTION_RELOCATION_ADD:
865 case ELF_SECTION_RELOCATION:
866 add_relocation_table (em, s);
875 void elf_set_dynamic_entries (elf_main_t * em)
879 /* Start address for sections may have changed. */
881 elf64_dynamic_entry_t * e;
883 vec_foreach (e, em->dynamic_entries)
887 case ELF_DYNAMIC_ENTRY_INIT_FUNCTION:
888 case ELF_DYNAMIC_ENTRY_FINI_FUNCTION:
889 case ELF_DYNAMIC_ENTRY_SYMBOL_HASH:
890 case ELF_DYNAMIC_ENTRY_GNU_HASH:
891 case ELF_DYNAMIC_ENTRY_STRING_TABLE:
892 case ELF_DYNAMIC_ENTRY_SYMBOL_TABLE:
893 case ELF_DYNAMIC_ENTRY_PLT_GOT:
894 case ELF_DYNAMIC_ENTRY_PLT_RELOCATION_ADDRESS:
895 case ELF_DYNAMIC_ENTRY_RELA_ADDRESS:
896 case ELF_DYNAMIC_ENTRY_VERSION_NEED:
897 case ELF_DYNAMIC_ENTRY_VERSYM:
899 elf_section_t * es = elf_get_section_by_start_address_no_check (em, e->data);
900 /* If section is not found just leave e->data alone. */
902 e->data = es->header.exec_address;
912 if (em->first_header.file_class == ELF_64BIT)
914 elf64_dynamic_entry_t * e, * es;
916 es = em->dynamic_entries;
917 if (em->need_byte_swap)
922 e->type = elf_swap_u64 (em, e->type);
923 e->data = elf_swap_u64 (em, e->data);
927 elf_set_section_contents (em, em->dynamic_section_index, es, vec_bytes (es));
928 if (es != em->dynamic_entries)
933 elf32_dynamic_entry_t * es;
935 vec_clone (es, em->dynamic_entries);
936 if (em->need_byte_swap)
938 for (i = 0; i < vec_len (es); i++)
940 es[i].type = elf_swap_u32 (em, em->dynamic_entries[i].type);
941 es[i].data = elf_swap_u32 (em, em->dynamic_entries[i].data);
945 elf_set_section_contents (em, em->dynamic_section_index, es, vec_bytes (es));
951 elf_parse (elf_main_t * em,
955 elf_first_header_t * h = data;
956 elf64_file_header_t * fh = &em->file_header;
957 clib_error_t * error = 0;
960 char * save = em->file_name;
961 memset (em, 0, sizeof (em[0]));
962 em->file_name = save;
965 em->first_header = h[0];
967 CLIB_ARCH_IS_BIG_ENDIAN != (h->data_encoding == ELF_TWOS_COMPLEMENT_BIG_ENDIAN);
968 elf_swap_first_header (em, &em->first_header);
970 if (! (h->magic[0] == 0x7f
971 && h->magic[1] == 'E'
972 && h->magic[2] == 'L'
973 && h->magic[3] == 'F'))
974 return clib_error_return (0, "`%s': bad magic", em->file_name);
976 if (h->file_class == ELF_64BIT)
978 elf64_file_header_t * h64 = (void *) (h + 1);
979 #define _(t,f) fh->f = elf_swap_##t (em, h64->f);
980 foreach_elf64_file_header
985 elf32_file_header_t * h32 = (void *) (h + 1);
987 #define _(t,f) fh->f = elf_swap_##t (em, h32->f);
988 foreach_elf32_file_header
992 elf_parse_segments (em, data);
993 elf_parse_sections (em, data);
995 /* Figure which sections are contained in each segment. */
999 vec_foreach (g, em->segments)
1004 if (g->header.memory_size == 0)
1007 g_lo = g->header.virtual_address;
1008 g_hi = g_lo + g->header.memory_size;
1010 vec_foreach (s, em->sections)
1012 s_lo = s->header.exec_address;
1013 s_hi = s_lo + s->header.file_size;
1015 if (s_lo >= g_lo && s_hi <= g_hi)
1017 g->section_index_bitmap = clib_bitmap_ori (g->section_index_bitmap, s->index);
1018 s->segment_index_bitmap = clib_bitmap_ori (s->segment_index_bitmap, g->index);
1030 add_dynamic_entries (elf_main_t * em, elf_section_t * s)
1034 /* Can't have more than one dynamic section. */
1035 ASSERT (em->dynamic_section_index == 0);
1036 em->dynamic_section_index = s->index;
1038 if (em->first_header.file_class == ELF_64BIT)
1040 elf64_dynamic_entry_t * e;
1042 e = elf_get_section_contents (em, s - em->sections, sizeof (e[0]));
1043 if (em->need_byte_swap)
1044 for (i = 0; i < vec_len (e); i++)
1046 e[i].type = elf_swap_u64 (em, e[i].type);
1047 e[i].data = elf_swap_u64 (em, e[i].data);
1050 em->dynamic_entries = e;
1054 elf32_dynamic_entry_t * e;
1056 e = elf_get_section_contents (em, s - em->sections, sizeof (e[0]));
1057 vec_clone (em->dynamic_entries, e);
1058 if (em->need_byte_swap)
1059 for (i = 0; i < vec_len (e); i++)
1061 em->dynamic_entries[i].type = elf_swap_u32 (em, e[i].type);
1062 em->dynamic_entries[i].data = elf_swap_u32 (em, e[i].data);
1069 static void byte_swap_verneed (elf_main_t * em,
1070 elf_dynamic_version_need_union_t * vus)
1072 uword * entries_swapped = 0;
1075 for (i = 0; i < vec_len (vus); i++)
1077 elf_dynamic_version_need_union_t * n = vec_elt_at_index (vus, i);
1078 elf_dynamic_version_need_union_t * a;
1080 if (clib_bitmap_get (entries_swapped, i))
1083 elf_swap_verneed (&n->need);
1084 entries_swapped = clib_bitmap_set (entries_swapped, i, 1);
1086 if (n->need.first_aux_offset != 0)
1088 ASSERT (n->need.first_aux_offset % sizeof (n[0]) == 0);
1089 j = i + (n->need.first_aux_offset / sizeof (n[0]));
1092 a = vec_elt_at_index (vus, j);
1093 if (! clib_bitmap_get (entries_swapped, j))
1095 entries_swapped = clib_bitmap_set (entries_swapped, j, 1);
1096 elf_swap_verneed_aux (&a->aux);
1098 if (a->aux.next_offset == 0)
1100 ASSERT (a->aux.next_offset % sizeof (a->aux) == 0);
1101 j += (a->aux.next_offset / sizeof (a->aux));
1106 clib_bitmap_free (entries_swapped);
1110 set_dynamic_verneed (elf_main_t * em)
1112 elf_dynamic_version_need_union_t * vus = em->verneed;
1114 if (em->need_byte_swap)
1116 vus = vec_dup (vus);
1117 byte_swap_verneed (em, vus);
1120 elf_set_section_contents (em, em->verneed_section_index, vus, vec_bytes (vus));
1121 if (vus != em->verneed)
1126 set_symbol_table (elf_main_t * em, u32 table_index)
1128 elf_symbol_table_t * tab = vec_elt_at_index (em->symbol_tables, table_index);
1130 if (em->first_header.file_class == ELF_64BIT)
1132 elf64_symbol_t * s, * syms;
1134 syms = vec_dup (tab->symbols);
1135 vec_foreach (s, syms)
1137 #define _(t,f) s->f = elf_swap_##t (em, s->f);
1138 foreach_elf64_symbol_header;
1142 elf_set_section_contents (em, tab->section_index,
1143 syms, vec_bytes (syms));
1147 elf32_symbol_t * syms;
1149 vec_clone (syms, tab->symbols);
1150 for (i = 0; i < vec_len (tab->symbols); i++)
1152 #define _(t,f) syms[i].f = elf_swap_##t (em, tab->symbols[i].f);
1153 foreach_elf32_symbol_header;
1157 elf_set_section_contents (em, tab->section_index,
1158 syms, vec_bytes (syms));
1163 elf_find_interpreter (elf_main_t * em, void * data)
1169 vec_foreach (g, em->segments)
1171 if (g->header.type == ELF_SEGMENT_INTERP)
1175 if (g >= vec_end (em->segments))
1178 p = hash_get (em->section_by_start_address, g->header.virtual_address);
1182 s = vec_elt_at_index (em->sections, p[0]);
1183 return (char *) vec_dup (s->contents);
1187 elf_get_section_contents_with_starting_address (elf_main_t * em,
1188 uword start_address,
1190 u32 * section_index_result)
1193 clib_error_t * error;
1195 error = elf_get_section_by_start_address (em, start_address, &s);
1198 clib_error_report (error);
1202 if (section_index_result)
1203 *section_index_result = s->index;
1205 return elf_get_section_contents (em, s->index, elt_size);
1208 static void elf_parse_dynamic (elf_main_t * em)
1211 elf64_dynamic_entry_t * e;
1213 vec_foreach (s, em->sections)
1215 switch (s->header.type)
1217 case ELF_SECTION_DYNAMIC:
1218 add_dynamic_entries (em, s);
1226 em->dynamic_string_table_section_index = ~0;
1227 em->dynamic_string_table = 0;
1229 vec_foreach (e, em->dynamic_entries)
1233 case ELF_DYNAMIC_ENTRY_STRING_TABLE:
1234 ASSERT (vec_len (em->dynamic_string_table) == 0);
1235 em->dynamic_string_table
1236 = elf_get_section_contents_with_starting_address (em, e->data, sizeof (u8),
1237 &em->dynamic_string_table_section_index);
1240 case ELF_DYNAMIC_ENTRY_SYMBOL_TABLE:
1243 clib_error_t * error;
1245 error = elf_get_section_by_start_address (em, e->data, &s);
1248 clib_error_report (error);
1252 em->dynamic_symbol_table_section_index = s - em->sections;
1256 case ELF_DYNAMIC_ENTRY_VERSYM:
1258 = elf_get_section_contents_with_starting_address (em, e->data, sizeof (em->versym[0]),
1259 &em->versym_section_index);
1260 if (em->need_byte_swap)
1263 for (i = 0; i < vec_len (em->versym); i++)
1264 em->versym[i] = clib_byte_swap_u16 (em->versym[i]);
1268 case ELF_DYNAMIC_ENTRY_VERSION_NEED:
1270 = elf_get_section_contents_with_starting_address (em, e->data, sizeof (em->verneed[0]),
1271 &em->verneed_section_index);
1272 if (em->need_byte_swap)
1273 byte_swap_verneed (em, em->verneed);
1282 #include <sys/types.h>
1283 #include <sys/stat.h>
1286 clib_error_t * elf_read_file (elf_main_t * em, char * file_name)
1289 struct stat fd_stat;
1290 uword mmap_length = 0;
1292 clib_error_t * error = 0;
1296 fd = open (file_name, 0);
1299 error = clib_error_return_unix (0, "open `%s'", file_name);
1303 if (fstat (fd, &fd_stat) < 0)
1305 error = clib_error_return_unix (0, "fstat `%s'", file_name);
1308 mmap_length = fd_stat.st_size;
1310 data = mmap (0, mmap_length, PROT_READ, MAP_SHARED, fd, /* offset */ 0);
1311 if (~pointer_to_uword (data) == 0)
1313 error = clib_error_return_unix (0, "mmap `%s'", file_name);
1317 em->file_name = file_name;
1319 error = elf_parse (em, data, mmap_length);
1323 elf_parse_symbols (em);
1324 elf_parse_dynamic (em);
1326 em->interpreter = elf_find_interpreter (em, data);
1328 munmap (data, mmap_length);
1331 return /* no error */ 0;
1338 munmap (data, mmap_length);
1348 } string_table_builder_t;
1350 static u32 string_table_add_name (string_table_builder_t * b, u8 * n)
1354 p = hash_get_mem (b->hash, n);
1357 l = strlen ((char *) n);
1358 i = vec_len (b->new_table);
1359 vec_add (b->new_table, n, l + 1);
1361 for (j = 0; j <= l; j++)
1365 p = hash_get_mem (b->hash, n + j);
1367 /* Sub-string already in table? */
1372 hash_set_mem (b->hash, n + j, i + j);
1378 static u32 string_table_add_name_index (string_table_builder_t * b, u32 index)
1380 u8 * n = b->old_table + index;
1381 return string_table_add_name (b, n);
1384 static void string_table_init (string_table_builder_t * b, u8 * old_table)
1386 memset (b, 0, sizeof (b[0]));
1387 b->old_table = old_table;
1388 b->hash = hash_create_string (0, sizeof (uword));
1391 static u8 * string_table_done (string_table_builder_t * b)
1393 hash_free (b->hash);
1394 return b->new_table;
1397 static void layout_sections (elf_main_t * em)
1400 u32 n_sections_with_changed_exec_address = 0;
1401 u32 * deferred_symbol_and_string_sections = 0;
1402 u32 n_deleted_sections = 0;
1405 /* Re-build section string table (sections may have been deleted). */
1410 vec_foreach (s, em->sections)
1413 if (s->header.type == ~0)
1415 name = elf_section_name (em, s);
1416 s->header.name = vec_len (st);
1417 vec_add (st, name, strlen ((char *) name) + 1);
1420 s = vec_elt_at_index (em->sections, em->file_header.section_header_string_table_index);
1422 vec_free (s->contents);
1426 /* Re-build dynamic string table. */
1427 if (rebuild && em->dynamic_string_table_section_index != ~0)
1429 string_table_builder_t b;
1431 string_table_init (&b, em->dynamic_string_table);
1433 /* Add all dynamic symbols. */
1435 elf_symbol_table_t * symtab;
1436 elf64_symbol_t * sym;
1438 symtab = vec_elt_at_index (em->symbol_tables, em->dynamic_symbol_table_index);
1439 vec_foreach (sym, symtab->symbols)
1441 u8 * name = elf_symbol_name (symtab, sym);
1442 sym->name = string_table_add_name (&b, name);
1445 set_symbol_table (em, em->dynamic_symbol_table_index);
1448 /* Add all dynamic entries. */
1450 elf64_dynamic_entry_t * e;
1452 vec_foreach (e, em->dynamic_entries)
1456 case ELF_DYNAMIC_ENTRY_NEEDED_LIBRARY:
1457 case ELF_DYNAMIC_ENTRY_RPATH:
1458 case ELF_DYNAMIC_ENTRY_RUN_PATH:
1459 e->data = string_table_add_name_index (&b, e->data);
1465 /* Add all version needs. */
1466 if (vec_len (em->verneed) > 0)
1468 elf_dynamic_version_need_union_t * n, * a;
1473 n->need.file_name_offset = string_table_add_name_index (&b, n->need.file_name_offset);
1475 if (n->need.first_aux_offset != 0)
1477 a = n + n->need.first_aux_offset / sizeof (n[0]);
1480 a->aux.name = string_table_add_name_index (&b, a->aux.name);
1481 if (a->aux.next_offset == 0)
1483 a += a->aux.next_offset / sizeof (a[0]);
1487 if (n->need.next_offset == 0)
1490 n += n->need.next_offset / sizeof (n[0]);
1493 set_dynamic_verneed (em);
1496 s = vec_elt_at_index (em->sections, em->dynamic_string_table_section_index);
1498 vec_free (s->contents);
1499 s->contents = string_table_done (&b);
1502 /* Figure file offsets and exec addresses for sections. */
1504 u64 exec_address = 0, file_offset = 0;
1505 u64 file_size, align_size;
1507 vec_foreach (s, em->sections)
1509 /* Ignore deleted and unused sections. */
1510 switch (s->header.type)
1513 n_deleted_sections++;
1514 case ELF_SECTION_UNUSED:
1517 case ELF_SECTION_STRING_TABLE:
1518 case ELF_SECTION_SYMBOL_TABLE:
1519 if (! (s->index == em->dynamic_string_table_section_index
1520 || s->index == em->file_header.section_header_string_table_index))
1522 vec_add1 (deferred_symbol_and_string_sections, s->index);
1531 exec_address = round_pow2_u64 (exec_address, s->header.align);
1533 /* Put sections we added at end of file. */
1534 if (s->header.file_offset == ~0)
1535 s->header.file_offset = file_offset;
1537 /* Follow gaps in original file. */
1538 if (s->header.exec_address > exec_address)
1540 exec_address = s->header.exec_address;
1541 file_offset = s->header.file_offset;
1544 if (s->header.flags & ELF_SECTION_FLAG_ALLOC)
1546 s->exec_address_change = exec_address - s->header.exec_address;
1547 n_sections_with_changed_exec_address += s->exec_address_change != 0;
1548 s->header.exec_address = exec_address;
1551 if (s->header.type == ELF_SECTION_NO_BITS)
1552 file_size = s->header.file_size;
1554 file_size = vec_len (s->contents);
1559 if (s + 1 >= vec_end (em->sections))
1561 else if (s[1].header.type == ELF_SECTION_NO_BITS)
1564 align = s[1].header.align;
1566 if (s->header.flags & ELF_SECTION_FLAG_ALLOC)
1568 u64 v = round_pow2_u64 (exec_address + file_size, align);
1569 align_size = v - exec_address;
1573 u64 v = round_pow2_u64 (file_offset + file_size, align);
1574 align_size = v - file_offset;
1578 s->header.file_offset = file_offset;
1579 s->header.file_size = file_size;
1580 s->align_size = align_size;
1582 if (s->header.type != ELF_SECTION_NO_BITS)
1583 file_offset += align_size;
1584 exec_address += align_size;
1587 /* Section headers go after last section but before symbol/string
1590 elf64_file_header_t * fh = &em->file_header;
1592 fh->section_header_file_offset = file_offset;
1593 fh->section_header_count = vec_len (em->sections) - n_deleted_sections;
1594 file_offset += fh->section_header_count * fh->section_header_size;
1599 for (i = 0; i < vec_len (deferred_symbol_and_string_sections); i++)
1601 s = vec_elt_at_index (em->sections, deferred_symbol_and_string_sections[i]);
1603 s->header.file_offset = file_offset;
1604 s->header.file_size = vec_len (s->contents);
1606 align_size = round_pow2 (vec_len (s->contents), 16);
1607 s->align_size = align_size;
1608 file_offset += align_size;
1610 vec_free (deferred_symbol_and_string_sections);
1614 /* Update dynamic entries now that sections have been assigned
1615 possibly new addresses. */
1617 elf_set_dynamic_entries (em);
1619 /* Update segments for changed section addresses. */
1624 vec_foreach (g, em->segments)
1626 u64 s_lo, s_hi, f_lo = 0;
1629 if (g->header.memory_size == 0)
1633 clib_bitmap_foreach (si, g->section_index_bitmap, ({
1636 s = vec_elt_at_index (em->sections, si);
1637 lo = s->header.exec_address;
1638 hi = lo + s->align_size;
1639 if (n_sections == 0)
1643 f_lo = s->header.file_offset;
1651 f_lo = s->header.file_offset;
1658 if (n_sections == 0)
1661 /* File offset zero includes ELF headers/segment headers.
1662 Don't change that. */
1663 if (g->header.file_offset == 0
1664 && g->header.type == ELF_SEGMENT_LOAD)
1666 s_lo = g->header.virtual_address;
1667 f_lo = g->header.file_offset;
1670 g->header.virtual_address = s_lo;
1671 g->header.physical_address = s_lo;
1672 g->header.file_offset = f_lo;
1673 g->header.memory_size = s_hi - s_lo;
1678 clib_error_t * elf_write_file (elf_main_t * em, char * file_name)
1682 clib_error_t * error = 0;
1684 fd = open (file_name, O_CREAT | O_RDWR | O_TRUNC, 0755);
1686 return clib_error_return_unix (0, "open `%s'", file_name);
1688 f = fdopen (fd, "w");
1690 /* Section contents may have changed. So, we need to update
1691 stuff to reflect this. */
1692 layout_sections (em);
1694 /* Write first header. */
1696 elf_first_header_t h = em->first_header;
1698 elf_swap_first_header (em, &h);
1699 if (fwrite (&h, sizeof (h), 1, f) != 1)
1701 error = clib_error_return_unix (0, "write first header");
1706 /* Write file header. */
1708 elf64_file_header_t h = em->file_header;
1710 /* Segment headers are after first header. */
1711 h.segment_header_file_offset = sizeof (elf_first_header_t);
1712 if (em->first_header.file_class == ELF_64BIT)
1713 h.segment_header_file_offset += sizeof (elf64_file_header_t);
1715 h.segment_header_file_offset += sizeof (elf32_file_header_t);
1717 if (em->first_header.file_class == ELF_64BIT)
1719 #define _(t,field) h.field = elf_swap_##t (em, h.field);
1720 foreach_elf64_file_header;
1723 if (fwrite (&h, sizeof (h), 1, f) != 1)
1725 error = clib_error_return_unix (0, "write file header");
1731 elf32_file_header_t h32;
1733 #define _(t,field) h32.field = elf_swap_##t (em, h.field);
1734 foreach_elf32_file_header;
1737 if (fwrite (&h32, sizeof (h32), 1, f) != 1)
1739 error = clib_error_return_unix (0, "write file header");
1745 /* Write segment headers. */
1749 vec_foreach (s, em->segments)
1751 elf64_segment_header_t h;
1753 if (s->header.type == ~0)
1758 if (em->first_header.file_class == ELF_64BIT)
1760 #define _(t,field) h.field = elf_swap_##t (em, h.field);
1761 foreach_elf64_segment_header;
1764 if (fwrite (&h, sizeof (h), 1, f) != 1)
1766 error = clib_error_return_unix (0, "write segment header %U", format_elf_segment, em, s);
1772 elf32_segment_header_t h32;
1774 #define _(t,field) h32.field = elf_swap_##t (em, h.field);
1775 foreach_elf32_segment_header;
1778 if (fwrite (&h32, sizeof (h32), 1, f) != 1)
1780 error = clib_error_return_unix (0, "write segment header %U", format_elf_segment, em, s);
1787 /* Write contents for all sections. */
1791 vec_foreach (s, em->sections)
1793 if (s->header.file_size == 0)
1796 if (fseek (f, s->header.file_offset, SEEK_SET) < 0)
1797 return clib_error_return_unix (0, "fseek 0x%Lx", s->header.file_offset);
1799 if (s->header.type == ELF_SECTION_NO_BITS)
1800 /* don't write for .bss sections */;
1801 else if (fwrite (s->contents, vec_len (s->contents), 1, f) != 1)
1803 error = clib_error_return_unix (0, "write %s section contents", elf_section_name (em, s));
1808 /* Finally write section headers. */
1809 if (fseek (f, em->file_header.section_header_file_offset, SEEK_SET) < 0)
1810 return clib_error_return_unix (0, "fseek 0x%Lx", em->file_header.section_header_file_offset);
1812 vec_foreach (s, em->sections)
1814 elf64_section_header_t h;
1816 if (s->header.type == ~0)
1821 if (em->first_header.file_class == ELF_64BIT)
1823 #define _(t,field) h.field = elf_swap_##t (em, h.field);
1824 foreach_elf64_section_header;
1827 if (fwrite (&h, sizeof (h), 1, f) != 1)
1829 error = clib_error_return_unix (0, "write %s section header", elf_section_name (em, s));
1835 elf32_section_header_t h32;
1837 #define _(t,field) h32.field = elf_swap_##t (em, h.field);
1838 foreach_elf32_section_header;
1841 if (fwrite (&h32, sizeof (h32), 1, f) != 1)
1843 error = clib_error_return_unix (0, "write %s section header", elf_section_name (em, s));
1855 clib_error_t * elf_delete_named_section (elf_main_t * em, char * section_name)
1858 clib_error_t * error;
1860 error = elf_get_section_by_name (em, section_name, &s);
1864 s->header.type = ~0;
1870 elf_create_section_with_contents (elf_main_t * em,
1871 char * section_name,
1872 elf64_section_header_t * header,
1874 uword n_content_bytes)
1876 elf_section_t * s, * sts;
1878 uword * p, is_new_section;
1880 /* See if section already exists with given name.
1881 If so, just replace contents. */
1883 if ((p = hash_get_mem (em->section_by_name, section_name)))
1885 s = vec_elt_at_index (em->sections, p[0]);
1886 _vec_len (s->contents) = 0;
1891 vec_add2 (em->sections, s, 1);
1896 sts = vec_elt_at_index (em->sections, em->file_header.section_header_string_table_index);
1899 s->header = header[0];
1901 s->header.file_offset = ~0;
1902 s->header.file_size = n_content_bytes;
1903 s->index = s - em->sections;
1905 /* Add name to string table. */
1906 s->header.name = vec_len (st);
1907 vec_add (st, section_name, strlen (section_name));
1911 vec_resize (c, n_content_bytes);
1912 memcpy (c, contents, n_content_bytes);
1915 em->file_header.section_header_count += is_new_section && s->header.type != ~0;
1918 uword elf_delete_segment_with_type (elf_main_t * em, elf_segment_type_t segment_type)
1920 uword n_deleted = 0;
1923 vec_foreach (s, em->segments)
1924 if (s->header.type == segment_type)
1926 s->header.type = ~0;
1930 ASSERT (em->file_header.segment_header_count >= n_deleted);
1931 em->file_header.segment_header_count -= n_deleted;
1936 #endif /* CLIB_UNIX */