Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * elf.c - ELF access library
0004  *
0005  * Adapted from kpatch (https://github.com/dynup/kpatch):
0006  * Copyright (C) 2013-2015 Josh Poimboeuf <jpoimboe@redhat.com>
0007  * Copyright (C) 2014 Seth Jennings <sjenning@redhat.com>
0008  */
0009 
0010 #include <sys/types.h>
0011 #include <sys/stat.h>
0012 #include <sys/mman.h>
0013 #include <fcntl.h>
0014 #include <stdio.h>
0015 #include <stdlib.h>
0016 #include <string.h>
0017 #include <unistd.h>
0018 #include <errno.h>
0019 #include <objtool/builtin.h>
0020 
0021 #include <objtool/elf.h>
0022 #include <objtool/warn.h>
0023 
0024 #define MAX_NAME_LEN 128
0025 
0026 static inline u32 str_hash(const char *str)
0027 {
0028     return jhash(str, strlen(str), 0);
0029 }
0030 
0031 #define __elf_table(name)   (elf->name##_hash)
0032 #define __elf_bits(name)    (elf->name##_bits)
0033 
0034 #define elf_hash_add(name, node, key) \
0035     hlist_add_head(node, &__elf_table(name)[hash_min(key, __elf_bits(name))])
0036 
0037 #define elf_hash_for_each_possible(name, obj, member, key) \
0038     hlist_for_each_entry(obj, &__elf_table(name)[hash_min(key, __elf_bits(name))], member)
0039 
0040 #define elf_alloc_hash(name, size) \
0041 ({ \
0042     __elf_bits(name) = max(10, ilog2(size)); \
0043     __elf_table(name) = mmap(NULL, sizeof(struct hlist_head) << __elf_bits(name), \
0044                  PROT_READ|PROT_WRITE, \
0045                  MAP_PRIVATE|MAP_ANON, -1, 0); \
0046     if (__elf_table(name) == (void *)-1L) { \
0047         WARN("mmap fail " #name); \
0048         __elf_table(name) = NULL; \
0049     } \
0050     __elf_table(name); \
0051 })
0052 
0053 static bool symbol_to_offset(struct rb_node *a, const struct rb_node *b)
0054 {
0055     struct symbol *sa = rb_entry(a, struct symbol, node);
0056     struct symbol *sb = rb_entry(b, struct symbol, node);
0057 
0058     if (sa->offset < sb->offset)
0059         return true;
0060     if (sa->offset > sb->offset)
0061         return false;
0062 
0063     if (sa->len < sb->len)
0064         return true;
0065     if (sa->len > sb->len)
0066         return false;
0067 
0068     sa->alias = sb;
0069 
0070     return false;
0071 }
0072 
0073 static int symbol_by_offset(const void *key, const struct rb_node *node)
0074 {
0075     const struct symbol *s = rb_entry(node, struct symbol, node);
0076     const unsigned long *o = key;
0077 
0078     if (*o < s->offset)
0079         return -1;
0080     if (*o >= s->offset + s->len)
0081         return 1;
0082 
0083     return 0;
0084 }
0085 
0086 struct symbol_hole {
0087     unsigned long key;
0088     const struct symbol *sym;
0089 };
0090 
0091 /*
0092  * Find !section symbol where @offset is after it.
0093  */
0094 static int symbol_hole_by_offset(const void *key, const struct rb_node *node)
0095 {
0096     const struct symbol *s = rb_entry(node, struct symbol, node);
0097     struct symbol_hole *sh = (void *)key;
0098 
0099     if (sh->key < s->offset)
0100         return -1;
0101 
0102     if (sh->key >= s->offset + s->len) {
0103         if (s->type != STT_SECTION)
0104             sh->sym = s;
0105         return 1;
0106     }
0107 
0108     return 0;
0109 }
0110 
0111 struct section *find_section_by_name(const struct elf *elf, const char *name)
0112 {
0113     struct section *sec;
0114 
0115     elf_hash_for_each_possible(section_name, sec, name_hash, str_hash(name)) {
0116         if (!strcmp(sec->name, name))
0117             return sec;
0118     }
0119 
0120     return NULL;
0121 }
0122 
0123 static struct section *find_section_by_index(struct elf *elf,
0124                          unsigned int idx)
0125 {
0126     struct section *sec;
0127 
0128     elf_hash_for_each_possible(section, sec, hash, idx) {
0129         if (sec->idx == idx)
0130             return sec;
0131     }
0132 
0133     return NULL;
0134 }
0135 
0136 static struct symbol *find_symbol_by_index(struct elf *elf, unsigned int idx)
0137 {
0138     struct symbol *sym;
0139 
0140     elf_hash_for_each_possible(symbol, sym, hash, idx) {
0141         if (sym->idx == idx)
0142             return sym;
0143     }
0144 
0145     return NULL;
0146 }
0147 
0148 struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset)
0149 {
0150     struct rb_node *node;
0151 
0152     rb_for_each(node, &offset, &sec->symbol_tree, symbol_by_offset) {
0153         struct symbol *s = rb_entry(node, struct symbol, node);
0154 
0155         if (s->offset == offset && s->type != STT_SECTION)
0156             return s;
0157     }
0158 
0159     return NULL;
0160 }
0161 
0162 struct symbol *find_func_by_offset(struct section *sec, unsigned long offset)
0163 {
0164     struct rb_node *node;
0165 
0166     rb_for_each(node, &offset, &sec->symbol_tree, symbol_by_offset) {
0167         struct symbol *s = rb_entry(node, struct symbol, node);
0168 
0169         if (s->offset == offset && s->type == STT_FUNC)
0170             return s;
0171     }
0172 
0173     return NULL;
0174 }
0175 
0176 struct symbol *find_symbol_containing(const struct section *sec, unsigned long offset)
0177 {
0178     struct rb_node *node;
0179 
0180     rb_for_each(node, &offset, &sec->symbol_tree, symbol_by_offset) {
0181         struct symbol *s = rb_entry(node, struct symbol, node);
0182 
0183         if (s->type != STT_SECTION)
0184             return s;
0185     }
0186 
0187     return NULL;
0188 }
0189 
0190 /*
0191  * Returns size of hole starting at @offset.
0192  */
0193 int find_symbol_hole_containing(const struct section *sec, unsigned long offset)
0194 {
0195     struct symbol_hole hole = {
0196         .key = offset,
0197         .sym = NULL,
0198     };
0199     struct rb_node *n;
0200     struct symbol *s;
0201 
0202     /*
0203      * Find the rightmost symbol for which @offset is after it.
0204      */
0205     n = rb_find(&hole, &sec->symbol_tree, symbol_hole_by_offset);
0206 
0207     /* found a symbol that contains @offset */
0208     if (n)
0209         return 0; /* not a hole */
0210 
0211     /* didn't find a symbol for which @offset is after it */
0212     if (!hole.sym)
0213         return 0; /* not a hole */
0214 
0215     /* @offset >= sym->offset + sym->len, find symbol after it */
0216     n = rb_next(&hole.sym->node);
0217     if (!n)
0218         return -1; /* until end of address space */
0219 
0220     /* hole until start of next symbol */
0221     s = rb_entry(n, struct symbol, node);
0222     return s->offset - offset;
0223 }
0224 
0225 struct symbol *find_func_containing(struct section *sec, unsigned long offset)
0226 {
0227     struct rb_node *node;
0228 
0229     rb_for_each(node, &offset, &sec->symbol_tree, symbol_by_offset) {
0230         struct symbol *s = rb_entry(node, struct symbol, node);
0231 
0232         if (s->type == STT_FUNC)
0233             return s;
0234     }
0235 
0236     return NULL;
0237 }
0238 
0239 struct symbol *find_symbol_by_name(const struct elf *elf, const char *name)
0240 {
0241     struct symbol *sym;
0242 
0243     elf_hash_for_each_possible(symbol_name, sym, name_hash, str_hash(name)) {
0244         if (!strcmp(sym->name, name))
0245             return sym;
0246     }
0247 
0248     return NULL;
0249 }
0250 
0251 struct reloc *find_reloc_by_dest_range(const struct elf *elf, struct section *sec,
0252                      unsigned long offset, unsigned int len)
0253 {
0254     struct reloc *reloc, *r = NULL;
0255     unsigned long o;
0256 
0257     if (!sec->reloc)
0258         return NULL;
0259 
0260     sec = sec->reloc;
0261 
0262     for_offset_range(o, offset, offset + len) {
0263         elf_hash_for_each_possible(reloc, reloc, hash,
0264                        sec_offset_hash(sec, o)) {
0265             if (reloc->sec != sec)
0266                 continue;
0267 
0268             if (reloc->offset >= offset && reloc->offset < offset + len) {
0269                 if (!r || reloc->offset < r->offset)
0270                     r = reloc;
0271             }
0272         }
0273         if (r)
0274             return r;
0275     }
0276 
0277     return NULL;
0278 }
0279 
0280 struct reloc *find_reloc_by_dest(const struct elf *elf, struct section *sec, unsigned long offset)
0281 {
0282     return find_reloc_by_dest_range(elf, sec, offset, 1);
0283 }
0284 
0285 static int read_sections(struct elf *elf)
0286 {
0287     Elf_Scn *s = NULL;
0288     struct section *sec;
0289     size_t shstrndx, sections_nr;
0290     int i;
0291 
0292     if (elf_getshdrnum(elf->elf, &sections_nr)) {
0293         WARN_ELF("elf_getshdrnum");
0294         return -1;
0295     }
0296 
0297     if (elf_getshdrstrndx(elf->elf, &shstrndx)) {
0298         WARN_ELF("elf_getshdrstrndx");
0299         return -1;
0300     }
0301 
0302     if (!elf_alloc_hash(section, sections_nr) ||
0303         !elf_alloc_hash(section_name, sections_nr))
0304         return -1;
0305 
0306     for (i = 0; i < sections_nr; i++) {
0307         sec = malloc(sizeof(*sec));
0308         if (!sec) {
0309             perror("malloc");
0310             return -1;
0311         }
0312         memset(sec, 0, sizeof(*sec));
0313 
0314         INIT_LIST_HEAD(&sec->symbol_list);
0315         INIT_LIST_HEAD(&sec->reloc_list);
0316 
0317         s = elf_getscn(elf->elf, i);
0318         if (!s) {
0319             WARN_ELF("elf_getscn");
0320             return -1;
0321         }
0322 
0323         sec->idx = elf_ndxscn(s);
0324 
0325         if (!gelf_getshdr(s, &sec->sh)) {
0326             WARN_ELF("gelf_getshdr");
0327             return -1;
0328         }
0329 
0330         sec->name = elf_strptr(elf->elf, shstrndx, sec->sh.sh_name);
0331         if (!sec->name) {
0332             WARN_ELF("elf_strptr");
0333             return -1;
0334         }
0335 
0336         if (sec->sh.sh_size != 0) {
0337             sec->data = elf_getdata(s, NULL);
0338             if (!sec->data) {
0339                 WARN_ELF("elf_getdata");
0340                 return -1;
0341             }
0342             if (sec->data->d_off != 0 ||
0343                 sec->data->d_size != sec->sh.sh_size) {
0344                 WARN("unexpected data attributes for %s",
0345                      sec->name);
0346                 return -1;
0347             }
0348         }
0349 
0350         if (sec->sh.sh_flags & SHF_EXECINSTR)
0351             elf->text_size += sec->sh.sh_size;
0352 
0353         list_add_tail(&sec->list, &elf->sections);
0354         elf_hash_add(section, &sec->hash, sec->idx);
0355         elf_hash_add(section_name, &sec->name_hash, str_hash(sec->name));
0356     }
0357 
0358     if (opts.stats) {
0359         printf("nr_sections: %lu\n", (unsigned long)sections_nr);
0360         printf("section_bits: %d\n", elf->section_bits);
0361     }
0362 
0363     /* sanity check, one more call to elf_nextscn() should return NULL */
0364     if (elf_nextscn(elf->elf, s)) {
0365         WARN("section entry mismatch");
0366         return -1;
0367     }
0368 
0369     return 0;
0370 }
0371 
0372 static void elf_add_symbol(struct elf *elf, struct symbol *sym)
0373 {
0374     struct list_head *entry;
0375     struct rb_node *pnode;
0376 
0377     INIT_LIST_HEAD(&sym->pv_target);
0378     sym->alias = sym;
0379 
0380     sym->type = GELF_ST_TYPE(sym->sym.st_info);
0381     sym->bind = GELF_ST_BIND(sym->sym.st_info);
0382 
0383     if (sym->type == STT_FILE)
0384         elf->num_files++;
0385 
0386     sym->offset = sym->sym.st_value;
0387     sym->len = sym->sym.st_size;
0388 
0389     rb_add(&sym->node, &sym->sec->symbol_tree, symbol_to_offset);
0390     pnode = rb_prev(&sym->node);
0391     if (pnode)
0392         entry = &rb_entry(pnode, struct symbol, node)->list;
0393     else
0394         entry = &sym->sec->symbol_list;
0395     list_add(&sym->list, entry);
0396     elf_hash_add(symbol, &sym->hash, sym->idx);
0397     elf_hash_add(symbol_name, &sym->name_hash, str_hash(sym->name));
0398 
0399     /*
0400      * Don't store empty STT_NOTYPE symbols in the rbtree.  They
0401      * can exist within a function, confusing the sorting.
0402      */
0403     if (!sym->len)
0404         rb_erase(&sym->node, &sym->sec->symbol_tree);
0405 }
0406 
0407 static int read_symbols(struct elf *elf)
0408 {
0409     struct section *symtab, *symtab_shndx, *sec;
0410     struct symbol *sym, *pfunc;
0411     int symbols_nr, i;
0412     char *coldstr;
0413     Elf_Data *shndx_data = NULL;
0414     Elf32_Word shndx;
0415 
0416     symtab = find_section_by_name(elf, ".symtab");
0417     if (symtab) {
0418         symtab_shndx = find_section_by_name(elf, ".symtab_shndx");
0419         if (symtab_shndx)
0420             shndx_data = symtab_shndx->data;
0421 
0422         symbols_nr = symtab->sh.sh_size / symtab->sh.sh_entsize;
0423     } else {
0424         /*
0425          * A missing symbol table is actually possible if it's an empty
0426          * .o file. This can happen for thunk_64.o. Make sure to at
0427          * least allocate the symbol hash tables so we can do symbol
0428          * lookups without crashing.
0429          */
0430         symbols_nr = 0;
0431     }
0432 
0433     if (!elf_alloc_hash(symbol, symbols_nr) ||
0434         !elf_alloc_hash(symbol_name, symbols_nr))
0435         return -1;
0436 
0437     for (i = 0; i < symbols_nr; i++) {
0438         sym = malloc(sizeof(*sym));
0439         if (!sym) {
0440             perror("malloc");
0441             return -1;
0442         }
0443         memset(sym, 0, sizeof(*sym));
0444 
0445         sym->idx = i;
0446 
0447         if (!gelf_getsymshndx(symtab->data, shndx_data, i, &sym->sym,
0448                       &shndx)) {
0449             WARN_ELF("gelf_getsymshndx");
0450             goto err;
0451         }
0452 
0453         sym->name = elf_strptr(elf->elf, symtab->sh.sh_link,
0454                        sym->sym.st_name);
0455         if (!sym->name) {
0456             WARN_ELF("elf_strptr");
0457             goto err;
0458         }
0459 
0460         if ((sym->sym.st_shndx > SHN_UNDEF &&
0461              sym->sym.st_shndx < SHN_LORESERVE) ||
0462             (shndx_data && sym->sym.st_shndx == SHN_XINDEX)) {
0463             if (sym->sym.st_shndx != SHN_XINDEX)
0464                 shndx = sym->sym.st_shndx;
0465 
0466             sym->sec = find_section_by_index(elf, shndx);
0467             if (!sym->sec) {
0468                 WARN("couldn't find section for symbol %s",
0469                      sym->name);
0470                 goto err;
0471             }
0472             if (GELF_ST_TYPE(sym->sym.st_info) == STT_SECTION) {
0473                 sym->name = sym->sec->name;
0474                 sym->sec->sym = sym;
0475             }
0476         } else
0477             sym->sec = find_section_by_index(elf, 0);
0478 
0479         elf_add_symbol(elf, sym);
0480     }
0481 
0482     if (opts.stats) {
0483         printf("nr_symbols: %lu\n", (unsigned long)symbols_nr);
0484         printf("symbol_bits: %d\n", elf->symbol_bits);
0485     }
0486 
0487     /* Create parent/child links for any cold subfunctions */
0488     list_for_each_entry(sec, &elf->sections, list) {
0489         list_for_each_entry(sym, &sec->symbol_list, list) {
0490             char pname[MAX_NAME_LEN + 1];
0491             size_t pnamelen;
0492             if (sym->type != STT_FUNC)
0493                 continue;
0494 
0495             if (sym->pfunc == NULL)
0496                 sym->pfunc = sym;
0497 
0498             if (sym->cfunc == NULL)
0499                 sym->cfunc = sym;
0500 
0501             coldstr = strstr(sym->name, ".cold");
0502             if (!coldstr)
0503                 continue;
0504 
0505             pnamelen = coldstr - sym->name;
0506             if (pnamelen > MAX_NAME_LEN) {
0507                 WARN("%s(): parent function name exceeds maximum length of %d characters",
0508                      sym->name, MAX_NAME_LEN);
0509                 return -1;
0510             }
0511 
0512             strncpy(pname, sym->name, pnamelen);
0513             pname[pnamelen] = '\0';
0514             pfunc = find_symbol_by_name(elf, pname);
0515 
0516             if (!pfunc) {
0517                 WARN("%s(): can't find parent function",
0518                      sym->name);
0519                 return -1;
0520             }
0521 
0522             sym->pfunc = pfunc;
0523             pfunc->cfunc = sym;
0524 
0525             /*
0526              * Unfortunately, -fnoreorder-functions puts the child
0527              * inside the parent.  Remove the overlap so we can
0528              * have sane assumptions.
0529              *
0530              * Note that pfunc->len now no longer matches
0531              * pfunc->sym.st_size.
0532              */
0533             if (sym->sec == pfunc->sec &&
0534                 sym->offset >= pfunc->offset &&
0535                 sym->offset + sym->len == pfunc->offset + pfunc->len) {
0536                 pfunc->len -= sym->len;
0537             }
0538         }
0539     }
0540 
0541     return 0;
0542 
0543 err:
0544     free(sym);
0545     return -1;
0546 }
0547 
0548 static struct section *elf_create_reloc_section(struct elf *elf,
0549                         struct section *base,
0550                         int reltype);
0551 
0552 int elf_add_reloc(struct elf *elf, struct section *sec, unsigned long offset,
0553           unsigned int type, struct symbol *sym, s64 addend)
0554 {
0555     struct reloc *reloc;
0556 
0557     if (!sec->reloc && !elf_create_reloc_section(elf, sec, SHT_RELA))
0558         return -1;
0559 
0560     reloc = malloc(sizeof(*reloc));
0561     if (!reloc) {
0562         perror("malloc");
0563         return -1;
0564     }
0565     memset(reloc, 0, sizeof(*reloc));
0566 
0567     reloc->sec = sec->reloc;
0568     reloc->offset = offset;
0569     reloc->type = type;
0570     reloc->sym = sym;
0571     reloc->addend = addend;
0572 
0573     list_add_tail(&reloc->list, &sec->reloc->reloc_list);
0574     elf_hash_add(reloc, &reloc->hash, reloc_hash(reloc));
0575 
0576     sec->reloc->sh.sh_size += sec->reloc->sh.sh_entsize;
0577     sec->reloc->changed = true;
0578 
0579     return 0;
0580 }
0581 
0582 /*
0583  * Ensure that any reloc section containing references to @sym is marked
0584  * changed such that it will get re-generated in elf_rebuild_reloc_sections()
0585  * with the new symbol index.
0586  */
0587 static void elf_dirty_reloc_sym(struct elf *elf, struct symbol *sym)
0588 {
0589     struct section *sec;
0590 
0591     list_for_each_entry(sec, &elf->sections, list) {
0592         struct reloc *reloc;
0593 
0594         if (sec->changed)
0595             continue;
0596 
0597         list_for_each_entry(reloc, &sec->reloc_list, list) {
0598             if (reloc->sym == sym) {
0599                 sec->changed = true;
0600                 break;
0601             }
0602         }
0603     }
0604 }
0605 
0606 /*
0607  * The libelf API is terrible; gelf_update_sym*() takes a data block relative
0608  * index value, *NOT* the symbol index. As such, iterate the data blocks and
0609  * adjust index until it fits.
0610  *
0611  * If no data block is found, allow adding a new data block provided the index
0612  * is only one past the end.
0613  */
0614 static int elf_update_symbol(struct elf *elf, struct section *symtab,
0615                  struct section *symtab_shndx, struct symbol *sym)
0616 {
0617     Elf32_Word shndx = sym->sec ? sym->sec->idx : SHN_UNDEF;
0618     Elf_Data *symtab_data = NULL, *shndx_data = NULL;
0619     Elf64_Xword entsize = symtab->sh.sh_entsize;
0620     int max_idx, idx = sym->idx;
0621     Elf_Scn *s, *t = NULL;
0622 
0623     s = elf_getscn(elf->elf, symtab->idx);
0624     if (!s) {
0625         WARN_ELF("elf_getscn");
0626         return -1;
0627     }
0628 
0629     if (symtab_shndx) {
0630         t = elf_getscn(elf->elf, symtab_shndx->idx);
0631         if (!t) {
0632             WARN_ELF("elf_getscn");
0633             return -1;
0634         }
0635     }
0636 
0637     for (;;) {
0638         /* get next data descriptor for the relevant sections */
0639         symtab_data = elf_getdata(s, symtab_data);
0640         if (t)
0641             shndx_data = elf_getdata(t, shndx_data);
0642 
0643         /* end-of-list */
0644         if (!symtab_data) {
0645             void *buf;
0646 
0647             if (idx) {
0648                 /* we don't do holes in symbol tables */
0649                 WARN("index out of range");
0650                 return -1;
0651             }
0652 
0653             /* if @idx == 0, it's the next contiguous entry, create it */
0654             symtab_data = elf_newdata(s);
0655             if (t)
0656                 shndx_data = elf_newdata(t);
0657 
0658             buf = calloc(1, entsize);
0659             if (!buf) {
0660                 WARN("malloc");
0661                 return -1;
0662             }
0663 
0664             symtab_data->d_buf = buf;
0665             symtab_data->d_size = entsize;
0666             symtab_data->d_align = 1;
0667             symtab_data->d_type = ELF_T_SYM;
0668 
0669             symtab->sh.sh_size += entsize;
0670             symtab->changed = true;
0671 
0672             if (t) {
0673                 shndx_data->d_buf = &sym->sec->idx;
0674                 shndx_data->d_size = sizeof(Elf32_Word);
0675                 shndx_data->d_align = sizeof(Elf32_Word);
0676                 shndx_data->d_type = ELF_T_WORD;
0677 
0678                 symtab_shndx->sh.sh_size += sizeof(Elf32_Word);
0679                 symtab_shndx->changed = true;
0680             }
0681 
0682             break;
0683         }
0684 
0685         /* empty blocks should not happen */
0686         if (!symtab_data->d_size) {
0687             WARN("zero size data");
0688             return -1;
0689         }
0690 
0691         /* is this the right block? */
0692         max_idx = symtab_data->d_size / entsize;
0693         if (idx < max_idx)
0694             break;
0695 
0696         /* adjust index and try again */
0697         idx -= max_idx;
0698     }
0699 
0700     /* something went side-ways */
0701     if (idx < 0) {
0702         WARN("negative index");
0703         return -1;
0704     }
0705 
0706     /* setup extended section index magic and write the symbol */
0707     if (shndx >= SHN_UNDEF && shndx < SHN_LORESERVE) {
0708         sym->sym.st_shndx = shndx;
0709         if (!shndx_data)
0710             shndx = 0;
0711     } else {
0712         sym->sym.st_shndx = SHN_XINDEX;
0713         if (!shndx_data) {
0714             WARN("no .symtab_shndx");
0715             return -1;
0716         }
0717     }
0718 
0719     if (!gelf_update_symshndx(symtab_data, shndx_data, idx, &sym->sym, shndx)) {
0720         WARN_ELF("gelf_update_symshndx");
0721         return -1;
0722     }
0723 
0724     return 0;
0725 }
0726 
0727 static struct symbol *
0728 elf_create_section_symbol(struct elf *elf, struct section *sec)
0729 {
0730     struct section *symtab, *symtab_shndx;
0731     Elf32_Word first_non_local, new_idx;
0732     struct symbol *sym, *old;
0733 
0734     symtab = find_section_by_name(elf, ".symtab");
0735     if (symtab) {
0736         symtab_shndx = find_section_by_name(elf, ".symtab_shndx");
0737     } else {
0738         WARN("no .symtab");
0739         return NULL;
0740     }
0741 
0742     sym = calloc(1, sizeof(*sym));
0743     if (!sym) {
0744         perror("malloc");
0745         return NULL;
0746     }
0747 
0748     sym->name = sec->name;
0749     sym->sec = sec;
0750 
0751     // st_name 0
0752     sym->sym.st_info = GELF_ST_INFO(STB_LOCAL, STT_SECTION);
0753     // st_other 0
0754     // st_value 0
0755     // st_size 0
0756 
0757     /*
0758      * Move the first global symbol, as per sh_info, into a new, higher
0759      * symbol index. This fees up a spot for a new local symbol.
0760      */
0761     first_non_local = symtab->sh.sh_info;
0762     new_idx = symtab->sh.sh_size / symtab->sh.sh_entsize;
0763     old = find_symbol_by_index(elf, first_non_local);
0764     if (old) {
0765         old->idx = new_idx;
0766 
0767         hlist_del(&old->hash);
0768         elf_hash_add(symbol, &old->hash, old->idx);
0769 
0770         elf_dirty_reloc_sym(elf, old);
0771 
0772         if (elf_update_symbol(elf, symtab, symtab_shndx, old)) {
0773             WARN("elf_update_symbol move");
0774             return NULL;
0775         }
0776 
0777         new_idx = first_non_local;
0778     }
0779 
0780     sym->idx = new_idx;
0781     if (elf_update_symbol(elf, symtab, symtab_shndx, sym)) {
0782         WARN("elf_update_symbol");
0783         return NULL;
0784     }
0785 
0786     /*
0787      * Either way, we added a LOCAL symbol.
0788      */
0789     symtab->sh.sh_info += 1;
0790 
0791     elf_add_symbol(elf, sym);
0792 
0793     return sym;
0794 }
0795 
0796 int elf_add_reloc_to_insn(struct elf *elf, struct section *sec,
0797               unsigned long offset, unsigned int type,
0798               struct section *insn_sec, unsigned long insn_off)
0799 {
0800     struct symbol *sym = insn_sec->sym;
0801     int addend = insn_off;
0802 
0803     if (!sym) {
0804         /*
0805          * Due to how weak functions work, we must use section based
0806          * relocations. Symbol based relocations would result in the
0807          * weak and non-weak function annotations being overlaid on the
0808          * non-weak function after linking.
0809          */
0810         sym = elf_create_section_symbol(elf, insn_sec);
0811         if (!sym)
0812             return -1;
0813 
0814         insn_sec->sym = sym;
0815     }
0816 
0817     return elf_add_reloc(elf, sec, offset, type, sym, addend);
0818 }
0819 
0820 static int read_rel_reloc(struct section *sec, int i, struct reloc *reloc, unsigned int *symndx)
0821 {
0822     if (!gelf_getrel(sec->data, i, &reloc->rel)) {
0823         WARN_ELF("gelf_getrel");
0824         return -1;
0825     }
0826     reloc->type = GELF_R_TYPE(reloc->rel.r_info);
0827     reloc->addend = 0;
0828     reloc->offset = reloc->rel.r_offset;
0829     *symndx = GELF_R_SYM(reloc->rel.r_info);
0830     return 0;
0831 }
0832 
0833 static int read_rela_reloc(struct section *sec, int i, struct reloc *reloc, unsigned int *symndx)
0834 {
0835     if (!gelf_getrela(sec->data, i, &reloc->rela)) {
0836         WARN_ELF("gelf_getrela");
0837         return -1;
0838     }
0839     reloc->type = GELF_R_TYPE(reloc->rela.r_info);
0840     reloc->addend = reloc->rela.r_addend;
0841     reloc->offset = reloc->rela.r_offset;
0842     *symndx = GELF_R_SYM(reloc->rela.r_info);
0843     return 0;
0844 }
0845 
0846 static int read_relocs(struct elf *elf)
0847 {
0848     struct section *sec;
0849     struct reloc *reloc;
0850     int i;
0851     unsigned int symndx;
0852     unsigned long nr_reloc, max_reloc = 0, tot_reloc = 0;
0853 
0854     if (!elf_alloc_hash(reloc, elf->text_size / 16))
0855         return -1;
0856 
0857     list_for_each_entry(sec, &elf->sections, list) {
0858         if ((sec->sh.sh_type != SHT_RELA) &&
0859             (sec->sh.sh_type != SHT_REL))
0860             continue;
0861 
0862         sec->base = find_section_by_index(elf, sec->sh.sh_info);
0863         if (!sec->base) {
0864             WARN("can't find base section for reloc section %s",
0865                  sec->name);
0866             return -1;
0867         }
0868 
0869         sec->base->reloc = sec;
0870 
0871         nr_reloc = 0;
0872         for (i = 0; i < sec->sh.sh_size / sec->sh.sh_entsize; i++) {
0873             reloc = malloc(sizeof(*reloc));
0874             if (!reloc) {
0875                 perror("malloc");
0876                 return -1;
0877             }
0878             memset(reloc, 0, sizeof(*reloc));
0879             switch (sec->sh.sh_type) {
0880             case SHT_REL:
0881                 if (read_rel_reloc(sec, i, reloc, &symndx))
0882                     return -1;
0883                 break;
0884             case SHT_RELA:
0885                 if (read_rela_reloc(sec, i, reloc, &symndx))
0886                     return -1;
0887                 break;
0888             default: return -1;
0889             }
0890 
0891             reloc->sec = sec;
0892             reloc->idx = i;
0893             reloc->sym = find_symbol_by_index(elf, symndx);
0894             if (!reloc->sym) {
0895                 WARN("can't find reloc entry symbol %d for %s",
0896                      symndx, sec->name);
0897                 return -1;
0898             }
0899 
0900             list_add_tail(&reloc->list, &sec->reloc_list);
0901             elf_hash_add(reloc, &reloc->hash, reloc_hash(reloc));
0902 
0903             nr_reloc++;
0904         }
0905         max_reloc = max(max_reloc, nr_reloc);
0906         tot_reloc += nr_reloc;
0907     }
0908 
0909     if (opts.stats) {
0910         printf("max_reloc: %lu\n", max_reloc);
0911         printf("tot_reloc: %lu\n", tot_reloc);
0912         printf("reloc_bits: %d\n", elf->reloc_bits);
0913     }
0914 
0915     return 0;
0916 }
0917 
0918 struct elf *elf_open_read(const char *name, int flags)
0919 {
0920     struct elf *elf;
0921     Elf_Cmd cmd;
0922 
0923     elf_version(EV_CURRENT);
0924 
0925     elf = malloc(sizeof(*elf));
0926     if (!elf) {
0927         perror("malloc");
0928         return NULL;
0929     }
0930     memset(elf, 0, offsetof(struct elf, sections));
0931 
0932     INIT_LIST_HEAD(&elf->sections);
0933 
0934     elf->fd = open(name, flags);
0935     if (elf->fd == -1) {
0936         fprintf(stderr, "objtool: Can't open '%s': %s\n",
0937             name, strerror(errno));
0938         goto err;
0939     }
0940 
0941     if ((flags & O_ACCMODE) == O_RDONLY)
0942         cmd = ELF_C_READ_MMAP;
0943     else if ((flags & O_ACCMODE) == O_RDWR)
0944         cmd = ELF_C_RDWR;
0945     else /* O_WRONLY */
0946         cmd = ELF_C_WRITE;
0947 
0948     elf->elf = elf_begin(elf->fd, cmd, NULL);
0949     if (!elf->elf) {
0950         WARN_ELF("elf_begin");
0951         goto err;
0952     }
0953 
0954     if (!gelf_getehdr(elf->elf, &elf->ehdr)) {
0955         WARN_ELF("gelf_getehdr");
0956         goto err;
0957     }
0958 
0959     if (read_sections(elf))
0960         goto err;
0961 
0962     if (read_symbols(elf))
0963         goto err;
0964 
0965     if (read_relocs(elf))
0966         goto err;
0967 
0968     return elf;
0969 
0970 err:
0971     elf_close(elf);
0972     return NULL;
0973 }
0974 
0975 static int elf_add_string(struct elf *elf, struct section *strtab, char *str)
0976 {
0977     Elf_Data *data;
0978     Elf_Scn *s;
0979     int len;
0980 
0981     if (!strtab)
0982         strtab = find_section_by_name(elf, ".strtab");
0983     if (!strtab) {
0984         WARN("can't find .strtab section");
0985         return -1;
0986     }
0987 
0988     s = elf_getscn(elf->elf, strtab->idx);
0989     if (!s) {
0990         WARN_ELF("elf_getscn");
0991         return -1;
0992     }
0993 
0994     data = elf_newdata(s);
0995     if (!data) {
0996         WARN_ELF("elf_newdata");
0997         return -1;
0998     }
0999 
1000     data->d_buf = str;
1001     data->d_size = strlen(str) + 1;
1002     data->d_align = 1;
1003 
1004     len = strtab->sh.sh_size;
1005     strtab->sh.sh_size += data->d_size;
1006     strtab->changed = true;
1007 
1008     return len;
1009 }
1010 
1011 struct section *elf_create_section(struct elf *elf, const char *name,
1012                    unsigned int sh_flags, size_t entsize, int nr)
1013 {
1014     struct section *sec, *shstrtab;
1015     size_t size = entsize * nr;
1016     Elf_Scn *s;
1017 
1018     sec = malloc(sizeof(*sec));
1019     if (!sec) {
1020         perror("malloc");
1021         return NULL;
1022     }
1023     memset(sec, 0, sizeof(*sec));
1024 
1025     INIT_LIST_HEAD(&sec->symbol_list);
1026     INIT_LIST_HEAD(&sec->reloc_list);
1027 
1028     s = elf_newscn(elf->elf);
1029     if (!s) {
1030         WARN_ELF("elf_newscn");
1031         return NULL;
1032     }
1033 
1034     sec->name = strdup(name);
1035     if (!sec->name) {
1036         perror("strdup");
1037         return NULL;
1038     }
1039 
1040     sec->idx = elf_ndxscn(s);
1041     sec->changed = true;
1042 
1043     sec->data = elf_newdata(s);
1044     if (!sec->data) {
1045         WARN_ELF("elf_newdata");
1046         return NULL;
1047     }
1048 
1049     sec->data->d_size = size;
1050     sec->data->d_align = 1;
1051 
1052     if (size) {
1053         sec->data->d_buf = malloc(size);
1054         if (!sec->data->d_buf) {
1055             perror("malloc");
1056             return NULL;
1057         }
1058         memset(sec->data->d_buf, 0, size);
1059     }
1060 
1061     if (!gelf_getshdr(s, &sec->sh)) {
1062         WARN_ELF("gelf_getshdr");
1063         return NULL;
1064     }
1065 
1066     sec->sh.sh_size = size;
1067     sec->sh.sh_entsize = entsize;
1068     sec->sh.sh_type = SHT_PROGBITS;
1069     sec->sh.sh_addralign = 1;
1070     sec->sh.sh_flags = SHF_ALLOC | sh_flags;
1071 
1072     /* Add section name to .shstrtab (or .strtab for Clang) */
1073     shstrtab = find_section_by_name(elf, ".shstrtab");
1074     if (!shstrtab)
1075         shstrtab = find_section_by_name(elf, ".strtab");
1076     if (!shstrtab) {
1077         WARN("can't find .shstrtab or .strtab section");
1078         return NULL;
1079     }
1080     sec->sh.sh_name = elf_add_string(elf, shstrtab, sec->name);
1081     if (sec->sh.sh_name == -1)
1082         return NULL;
1083 
1084     list_add_tail(&sec->list, &elf->sections);
1085     elf_hash_add(section, &sec->hash, sec->idx);
1086     elf_hash_add(section_name, &sec->name_hash, str_hash(sec->name));
1087 
1088     elf->changed = true;
1089 
1090     return sec;
1091 }
1092 
1093 static struct section *elf_create_rel_reloc_section(struct elf *elf, struct section *base)
1094 {
1095     char *relocname;
1096     struct section *sec;
1097 
1098     relocname = malloc(strlen(base->name) + strlen(".rel") + 1);
1099     if (!relocname) {
1100         perror("malloc");
1101         return NULL;
1102     }
1103     strcpy(relocname, ".rel");
1104     strcat(relocname, base->name);
1105 
1106     sec = elf_create_section(elf, relocname, 0, sizeof(GElf_Rel), 0);
1107     free(relocname);
1108     if (!sec)
1109         return NULL;
1110 
1111     base->reloc = sec;
1112     sec->base = base;
1113 
1114     sec->sh.sh_type = SHT_REL;
1115     sec->sh.sh_addralign = 8;
1116     sec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx;
1117     sec->sh.sh_info = base->idx;
1118     sec->sh.sh_flags = SHF_INFO_LINK;
1119 
1120     return sec;
1121 }
1122 
1123 static struct section *elf_create_rela_reloc_section(struct elf *elf, struct section *base)
1124 {
1125     char *relocname;
1126     struct section *sec;
1127 
1128     relocname = malloc(strlen(base->name) + strlen(".rela") + 1);
1129     if (!relocname) {
1130         perror("malloc");
1131         return NULL;
1132     }
1133     strcpy(relocname, ".rela");
1134     strcat(relocname, base->name);
1135 
1136     sec = elf_create_section(elf, relocname, 0, sizeof(GElf_Rela), 0);
1137     free(relocname);
1138     if (!sec)
1139         return NULL;
1140 
1141     base->reloc = sec;
1142     sec->base = base;
1143 
1144     sec->sh.sh_type = SHT_RELA;
1145     sec->sh.sh_addralign = 8;
1146     sec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx;
1147     sec->sh.sh_info = base->idx;
1148     sec->sh.sh_flags = SHF_INFO_LINK;
1149 
1150     return sec;
1151 }
1152 
1153 static struct section *elf_create_reloc_section(struct elf *elf,
1154                      struct section *base,
1155                      int reltype)
1156 {
1157     switch (reltype) {
1158     case SHT_REL:  return elf_create_rel_reloc_section(elf, base);
1159     case SHT_RELA: return elf_create_rela_reloc_section(elf, base);
1160     default:       return NULL;
1161     }
1162 }
1163 
1164 static int elf_rebuild_rel_reloc_section(struct section *sec)
1165 {
1166     struct reloc *reloc;
1167     int idx = 0;
1168     void *buf;
1169 
1170     /* Allocate a buffer for relocations */
1171     buf = malloc(sec->sh.sh_size);
1172     if (!buf) {
1173         perror("malloc");
1174         return -1;
1175     }
1176 
1177     sec->data->d_buf = buf;
1178     sec->data->d_size = sec->sh.sh_size;
1179     sec->data->d_type = ELF_T_REL;
1180 
1181     idx = 0;
1182     list_for_each_entry(reloc, &sec->reloc_list, list) {
1183         reloc->rel.r_offset = reloc->offset;
1184         reloc->rel.r_info = GELF_R_INFO(reloc->sym->idx, reloc->type);
1185         if (!gelf_update_rel(sec->data, idx, &reloc->rel)) {
1186             WARN_ELF("gelf_update_rel");
1187             return -1;
1188         }
1189         idx++;
1190     }
1191 
1192     return 0;
1193 }
1194 
1195 static int elf_rebuild_rela_reloc_section(struct section *sec)
1196 {
1197     struct reloc *reloc;
1198     int idx = 0;
1199     void *buf;
1200 
1201     /* Allocate a buffer for relocations with addends */
1202     buf = malloc(sec->sh.sh_size);
1203     if (!buf) {
1204         perror("malloc");
1205         return -1;
1206     }
1207 
1208     sec->data->d_buf = buf;
1209     sec->data->d_size = sec->sh.sh_size;
1210     sec->data->d_type = ELF_T_RELA;
1211 
1212     idx = 0;
1213     list_for_each_entry(reloc, &sec->reloc_list, list) {
1214         reloc->rela.r_offset = reloc->offset;
1215         reloc->rela.r_addend = reloc->addend;
1216         reloc->rela.r_info = GELF_R_INFO(reloc->sym->idx, reloc->type);
1217         if (!gelf_update_rela(sec->data, idx, &reloc->rela)) {
1218             WARN_ELF("gelf_update_rela");
1219             return -1;
1220         }
1221         idx++;
1222     }
1223 
1224     return 0;
1225 }
1226 
1227 static int elf_rebuild_reloc_section(struct elf *elf, struct section *sec)
1228 {
1229     switch (sec->sh.sh_type) {
1230     case SHT_REL:  return elf_rebuild_rel_reloc_section(sec);
1231     case SHT_RELA: return elf_rebuild_rela_reloc_section(sec);
1232     default:       return -1;
1233     }
1234 }
1235 
1236 int elf_write_insn(struct elf *elf, struct section *sec,
1237            unsigned long offset, unsigned int len,
1238            const char *insn)
1239 {
1240     Elf_Data *data = sec->data;
1241 
1242     if (data->d_type != ELF_T_BYTE || data->d_off) {
1243         WARN("write to unexpected data for section: %s", sec->name);
1244         return -1;
1245     }
1246 
1247     memcpy(data->d_buf + offset, insn, len);
1248     elf_flagdata(data, ELF_C_SET, ELF_F_DIRTY);
1249 
1250     elf->changed = true;
1251 
1252     return 0;
1253 }
1254 
1255 int elf_write_reloc(struct elf *elf, struct reloc *reloc)
1256 {
1257     struct section *sec = reloc->sec;
1258 
1259     if (sec->sh.sh_type == SHT_REL) {
1260         reloc->rel.r_info = GELF_R_INFO(reloc->sym->idx, reloc->type);
1261         reloc->rel.r_offset = reloc->offset;
1262 
1263         if (!gelf_update_rel(sec->data, reloc->idx, &reloc->rel)) {
1264             WARN_ELF("gelf_update_rel");
1265             return -1;
1266         }
1267     } else {
1268         reloc->rela.r_info = GELF_R_INFO(reloc->sym->idx, reloc->type);
1269         reloc->rela.r_addend = reloc->addend;
1270         reloc->rela.r_offset = reloc->offset;
1271 
1272         if (!gelf_update_rela(sec->data, reloc->idx, &reloc->rela)) {
1273             WARN_ELF("gelf_update_rela");
1274             return -1;
1275         }
1276     }
1277 
1278     elf->changed = true;
1279 
1280     return 0;
1281 }
1282 
1283 int elf_write(struct elf *elf)
1284 {
1285     struct section *sec;
1286     Elf_Scn *s;
1287 
1288     if (opts.dryrun)
1289         return 0;
1290 
1291     /* Update changed relocation sections and section headers: */
1292     list_for_each_entry(sec, &elf->sections, list) {
1293         if (sec->changed) {
1294             s = elf_getscn(elf->elf, sec->idx);
1295             if (!s) {
1296                 WARN_ELF("elf_getscn");
1297                 return -1;
1298             }
1299             if (!gelf_update_shdr(s, &sec->sh)) {
1300                 WARN_ELF("gelf_update_shdr");
1301                 return -1;
1302             }
1303 
1304             if (sec->base &&
1305                 elf_rebuild_reloc_section(elf, sec)) {
1306                 WARN("elf_rebuild_reloc_section");
1307                 return -1;
1308             }
1309 
1310             sec->changed = false;
1311             elf->changed = true;
1312         }
1313     }
1314 
1315     /* Make sure the new section header entries get updated properly. */
1316     elf_flagelf(elf->elf, ELF_C_SET, ELF_F_DIRTY);
1317 
1318     /* Write all changes to the file. */
1319     if (elf_update(elf->elf, ELF_C_WRITE) < 0) {
1320         WARN_ELF("elf_update");
1321         return -1;
1322     }
1323 
1324     elf->changed = false;
1325 
1326     return 0;
1327 }
1328 
1329 void elf_close(struct elf *elf)
1330 {
1331     struct section *sec, *tmpsec;
1332     struct symbol *sym, *tmpsym;
1333     struct reloc *reloc, *tmpreloc;
1334 
1335     if (elf->elf)
1336         elf_end(elf->elf);
1337 
1338     if (elf->fd > 0)
1339         close(elf->fd);
1340 
1341     list_for_each_entry_safe(sec, tmpsec, &elf->sections, list) {
1342         list_for_each_entry_safe(sym, tmpsym, &sec->symbol_list, list) {
1343             list_del(&sym->list);
1344             hash_del(&sym->hash);
1345             free(sym);
1346         }
1347         list_for_each_entry_safe(reloc, tmpreloc, &sec->reloc_list, list) {
1348             list_del(&reloc->list);
1349             hash_del(&reloc->hash);
1350             free(reloc);
1351         }
1352         list_del(&sec->list);
1353         free(sec);
1354     }
1355 
1356     free(elf);
1357 }