Back to home page

LXR

 
 

    


0001 /*
0002  * sortextable.h
0003  *
0004  * Copyright 2011 - 2012 Cavium, Inc.
0005  *
0006  * Some of this code was taken out of recordmcount.h written by:
0007  *
0008  * Copyright 2009 John F. Reiser <jreiser@BitWagon.com>.  All rights reserved.
0009  * Copyright 2010 Steven Rostedt <srostedt@redhat.com>, Red Hat Inc.
0010  *
0011  *
0012  * Licensed under the GNU General Public License, version 2 (GPLv2).
0013  */
0014 
0015 #undef extable_ent_size
0016 #undef compare_extable
0017 #undef do_func
0018 #undef Elf_Addr
0019 #undef Elf_Ehdr
0020 #undef Elf_Shdr
0021 #undef Elf_Rel
0022 #undef Elf_Rela
0023 #undef Elf_Sym
0024 #undef ELF_R_SYM
0025 #undef Elf_r_sym
0026 #undef ELF_R_INFO
0027 #undef Elf_r_info
0028 #undef ELF_ST_BIND
0029 #undef ELF_ST_TYPE
0030 #undef fn_ELF_R_SYM
0031 #undef fn_ELF_R_INFO
0032 #undef uint_t
0033 #undef _r
0034 #undef _w
0035 
0036 #ifdef SORTEXTABLE_64
0037 # define extable_ent_size   16
0038 # define compare_extable    compare_extable_64
0039 # define do_func        do64
0040 # define Elf_Addr       Elf64_Addr
0041 # define Elf_Ehdr       Elf64_Ehdr
0042 # define Elf_Shdr       Elf64_Shdr
0043 # define Elf_Rel        Elf64_Rel
0044 # define Elf_Rela       Elf64_Rela
0045 # define Elf_Sym        Elf64_Sym
0046 # define ELF_R_SYM      ELF64_R_SYM
0047 # define Elf_r_sym      Elf64_r_sym
0048 # define ELF_R_INFO     ELF64_R_INFO
0049 # define Elf_r_info     Elf64_r_info
0050 # define ELF_ST_BIND        ELF64_ST_BIND
0051 # define ELF_ST_TYPE        ELF64_ST_TYPE
0052 # define fn_ELF_R_SYM       fn_ELF64_R_SYM
0053 # define fn_ELF_R_INFO      fn_ELF64_R_INFO
0054 # define uint_t         uint64_t
0055 # define _r         r8
0056 # define _w         w8
0057 #else
0058 # define extable_ent_size   8
0059 # define compare_extable    compare_extable_32
0060 # define do_func        do32
0061 # define Elf_Addr       Elf32_Addr
0062 # define Elf_Ehdr       Elf32_Ehdr
0063 # define Elf_Shdr       Elf32_Shdr
0064 # define Elf_Rel        Elf32_Rel
0065 # define Elf_Rela       Elf32_Rela
0066 # define Elf_Sym        Elf32_Sym
0067 # define ELF_R_SYM      ELF32_R_SYM
0068 # define Elf_r_sym      Elf32_r_sym
0069 # define ELF_R_INFO     ELF32_R_INFO
0070 # define Elf_r_info     Elf32_r_info
0071 # define ELF_ST_BIND        ELF32_ST_BIND
0072 # define ELF_ST_TYPE        ELF32_ST_TYPE
0073 # define fn_ELF_R_SYM       fn_ELF32_R_SYM
0074 # define fn_ELF_R_INFO      fn_ELF32_R_INFO
0075 # define uint_t         uint32_t
0076 # define _r         r
0077 # define _w         w
0078 #endif
0079 
0080 static int compare_extable(const void *a, const void *b)
0081 {
0082     Elf_Addr av = _r(a);
0083     Elf_Addr bv = _r(b);
0084 
0085     if (av < bv)
0086         return -1;
0087     if (av > bv)
0088         return 1;
0089     return 0;
0090 }
0091 
0092 static void
0093 do_func(Elf_Ehdr *ehdr, char const *const fname, table_sort_t custom_sort)
0094 {
0095     Elf_Shdr *shdr;
0096     Elf_Shdr *shstrtab_sec;
0097     Elf_Shdr *strtab_sec = NULL;
0098     Elf_Shdr *symtab_sec = NULL;
0099     Elf_Shdr *extab_sec = NULL;
0100     Elf_Sym *sym;
0101     const Elf_Sym *symtab;
0102     Elf32_Word *symtab_shndx_start = NULL;
0103     Elf_Sym *sort_needed_sym;
0104     Elf_Shdr *sort_needed_sec;
0105     Elf_Rel *relocs = NULL;
0106     int relocs_size = 0;
0107     uint32_t *sort_done_location;
0108     const char *secstrtab;
0109     const char *strtab;
0110     char *extab_image;
0111     int extab_index = 0;
0112     int i;
0113     int idx;
0114     unsigned int num_sections;
0115     unsigned int secindex_strings;
0116 
0117     shdr = (Elf_Shdr *)((char *)ehdr + _r(&ehdr->e_shoff));
0118 
0119     num_sections = r2(&ehdr->e_shnum);
0120     if (num_sections == SHN_UNDEF)
0121         num_sections = _r(&shdr[0].sh_size);
0122 
0123     secindex_strings = r2(&ehdr->e_shstrndx);
0124     if (secindex_strings == SHN_XINDEX)
0125         secindex_strings = r(&shdr[0].sh_link);
0126 
0127     shstrtab_sec = shdr + secindex_strings;
0128     secstrtab = (const char *)ehdr + _r(&shstrtab_sec->sh_offset);
0129     for (i = 0; i < num_sections; i++) {
0130         idx = r(&shdr[i].sh_name);
0131         if (strcmp(secstrtab + idx, "__ex_table") == 0) {
0132             extab_sec = shdr + i;
0133             extab_index = i;
0134         }
0135         if ((r(&shdr[i].sh_type) == SHT_REL ||
0136              r(&shdr[i].sh_type) == SHT_RELA) &&
0137             r(&shdr[i].sh_info) == extab_index) {
0138             relocs = (void *)ehdr + _r(&shdr[i].sh_offset);
0139             relocs_size = _r(&shdr[i].sh_size);
0140         }
0141         if (strcmp(secstrtab + idx, ".symtab") == 0)
0142             symtab_sec = shdr + i;
0143         if (strcmp(secstrtab + idx, ".strtab") == 0)
0144             strtab_sec = shdr + i;
0145         if (r(&shdr[i].sh_type) == SHT_SYMTAB_SHNDX)
0146             symtab_shndx_start = (Elf32_Word *)(
0147                 (const char *)ehdr + _r(&shdr[i].sh_offset));
0148     }
0149     if (strtab_sec == NULL) {
0150         fprintf(stderr, "no .strtab in  file: %s\n", fname);
0151         fail_file();
0152     }
0153     if (symtab_sec == NULL) {
0154         fprintf(stderr, "no .symtab in  file: %s\n", fname);
0155         fail_file();
0156     }
0157     symtab = (const Elf_Sym *)((const char *)ehdr +
0158                    _r(&symtab_sec->sh_offset));
0159     if (extab_sec == NULL) {
0160         fprintf(stderr, "no __ex_table in  file: %s\n", fname);
0161         fail_file();
0162     }
0163     strtab = (const char *)ehdr + _r(&strtab_sec->sh_offset);
0164 
0165     extab_image = (void *)ehdr + _r(&extab_sec->sh_offset);
0166 
0167     if (custom_sort) {
0168         custom_sort(extab_image, _r(&extab_sec->sh_size));
0169     } else {
0170         int num_entries = _r(&extab_sec->sh_size) / extable_ent_size;
0171         qsort(extab_image, num_entries,
0172               extable_ent_size, compare_extable);
0173     }
0174     /* If there were relocations, we no longer need them. */
0175     if (relocs)
0176         memset(relocs, 0, relocs_size);
0177 
0178     /* find main_extable_sort_needed */
0179     sort_needed_sym = NULL;
0180     for (i = 0; i < _r(&symtab_sec->sh_size) / sizeof(Elf_Sym); i++) {
0181         sym = (void *)ehdr + _r(&symtab_sec->sh_offset);
0182         sym += i;
0183         if (ELF_ST_TYPE(sym->st_info) != STT_OBJECT)
0184             continue;
0185         idx = r(&sym->st_name);
0186         if (strcmp(strtab + idx, "main_extable_sort_needed") == 0) {
0187             sort_needed_sym = sym;
0188             break;
0189         }
0190     }
0191     if (sort_needed_sym == NULL) {
0192         fprintf(stderr,
0193             "no main_extable_sort_needed symbol in  file: %s\n",
0194             fname);
0195         fail_file();
0196     }
0197     sort_needed_sec = &shdr[get_secindex(r2(&sym->st_shndx),
0198                          sort_needed_sym - symtab,
0199                          symtab_shndx_start)];
0200     sort_done_location = (void *)ehdr +
0201         _r(&sort_needed_sec->sh_offset) +
0202         _r(&sort_needed_sym->st_value) -
0203         _r(&sort_needed_sec->sh_addr);
0204 
0205 #if 0
0206     printf("sort done marker at %lx\n",
0207            (unsigned long)((char *)sort_done_location - (char *)ehdr));
0208 #endif
0209     /* We sorted it, clear the flag. */
0210     w(0, sort_done_location);
0211 }