elf.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * elf.c - ELF access library
  4. *
  5. * Adapted from kpatch (https://github.com/dynup/kpatch):
  6. * Copyright (C) 2013-2015 Josh Poimboeuf <jpoimboe@redhat.com>
  7. * Copyright (C) 2014 Seth Jennings <sjenning@redhat.com>
  8. */
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <sys/mman.h>
  12. #include <fcntl.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <unistd.h>
  17. #include <errno.h>
  18. #include <ctype.h>
  19. #include <linux/align.h>
  20. #include <linux/kernel.h>
  21. #include <linux/interval_tree_generic.h>
  22. #include <linux/log2.h>
  23. #include <objtool/builtin.h>
  24. #include <objtool/elf.h>
  25. #include <objtool/warn.h>
  26. static inline u32 str_hash(const char *str)
  27. {
  28. return jhash(str, strlen(str), 0);
  29. }
  30. #define __elf_table(name) (elf->name##_hash)
  31. #define __elf_bits(name) (elf->name##_bits)
  32. #define __elf_table_entry(name, key) \
  33. __elf_table(name)[hash_min(key, __elf_bits(name))]
  34. #define elf_hash_add(name, node, key) \
  35. ({ \
  36. struct elf_hash_node *__node = node; \
  37. __node->next = __elf_table_entry(name, key); \
  38. __elf_table_entry(name, key) = __node; \
  39. })
  40. static inline void __elf_hash_del(struct elf_hash_node *node,
  41. struct elf_hash_node **head)
  42. {
  43. struct elf_hash_node *cur, *prev;
  44. if (node == *head) {
  45. *head = node->next;
  46. return;
  47. }
  48. for (prev = NULL, cur = *head; cur; prev = cur, cur = cur->next) {
  49. if (cur == node) {
  50. prev->next = cur->next;
  51. break;
  52. }
  53. }
  54. }
  55. #define elf_hash_del(name, node, key) \
  56. __elf_hash_del(node, &__elf_table_entry(name, key))
  57. #define elf_list_entry(ptr, type, member) \
  58. ({ \
  59. typeof(ptr) __ptr = (ptr); \
  60. __ptr ? container_of(__ptr, type, member) : NULL; \
  61. })
  62. #define elf_hash_for_each_possible(name, obj, member, key) \
  63. for (obj = elf_list_entry(__elf_table_entry(name, key), typeof(*obj), member); \
  64. obj; \
  65. obj = elf_list_entry(obj->member.next, typeof(*(obj)), member))
  66. #define elf_alloc_hash(name, size) \
  67. ({ \
  68. __elf_bits(name) = max(10, ilog2(size)); \
  69. __elf_table(name) = mmap(NULL, sizeof(struct elf_hash_node *) << __elf_bits(name), \
  70. PROT_READ|PROT_WRITE, \
  71. MAP_PRIVATE|MAP_ANON, -1, 0); \
  72. if (__elf_table(name) == (void *)-1L) { \
  73. ERROR_GLIBC("mmap fail " #name); \
  74. __elf_table(name) = NULL; \
  75. } \
  76. __elf_table(name); \
  77. })
  78. static inline unsigned long __sym_start(struct symbol *s)
  79. {
  80. return s->offset;
  81. }
  82. static inline unsigned long __sym_last(struct symbol *s)
  83. {
  84. return s->offset + (s->len ? s->len - 1 : 0);
  85. }
  86. INTERVAL_TREE_DEFINE(struct symbol, node, unsigned long, __subtree_last,
  87. __sym_start, __sym_last, static inline __maybe_unused,
  88. __sym)
  89. #define __sym_for_each(_iter, _tree, _start, _end) \
  90. for (_iter = __sym_iter_first((_tree), (_start), (_end)); \
  91. _iter; _iter = __sym_iter_next(_iter, (_start), (_end)))
  92. struct symbol_hole {
  93. unsigned long key;
  94. const struct symbol *sym;
  95. };
  96. /*
  97. * Find the last symbol before @offset.
  98. */
  99. static int symbol_hole_by_offset(const void *key, const struct rb_node *node)
  100. {
  101. const struct symbol *s = rb_entry(node, struct symbol, node);
  102. struct symbol_hole *sh = (void *)key;
  103. if (sh->key < s->offset)
  104. return -1;
  105. if (sh->key >= s->offset + s->len) {
  106. sh->sym = s;
  107. return 1;
  108. }
  109. return 0;
  110. }
  111. struct section *find_section_by_name(const struct elf *elf, const char *name)
  112. {
  113. struct section *sec;
  114. elf_hash_for_each_possible(section_name, sec, name_hash, str_hash(name)) {
  115. if (!strcmp(sec->name, name))
  116. return sec;
  117. }
  118. return NULL;
  119. }
  120. static struct section *find_section_by_index(struct elf *elf,
  121. unsigned int idx)
  122. {
  123. struct section *sec;
  124. elf_hash_for_each_possible(section, sec, hash, idx) {
  125. if (sec->idx == idx)
  126. return sec;
  127. }
  128. return NULL;
  129. }
  130. static struct symbol *find_symbol_by_index(struct elf *elf, unsigned int idx)
  131. {
  132. struct symbol *sym;
  133. elf_hash_for_each_possible(symbol, sym, hash, idx) {
  134. if (sym->idx == idx)
  135. return sym;
  136. }
  137. return NULL;
  138. }
  139. struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset)
  140. {
  141. struct rb_root_cached *tree = (struct rb_root_cached *)&sec->symbol_tree;
  142. struct symbol *sym;
  143. __sym_for_each(sym, tree, offset, offset) {
  144. if (sym->offset == offset && !is_sec_sym(sym))
  145. return sym->alias;
  146. }
  147. return NULL;
  148. }
  149. struct symbol *find_func_by_offset(struct section *sec, unsigned long offset)
  150. {
  151. struct rb_root_cached *tree = (struct rb_root_cached *)&sec->symbol_tree;
  152. struct symbol *func;
  153. __sym_for_each(func, tree, offset, offset) {
  154. if (func->offset == offset && is_func_sym(func))
  155. return func->alias;
  156. }
  157. return NULL;
  158. }
  159. struct symbol *find_symbol_containing(const struct section *sec, unsigned long offset)
  160. {
  161. struct rb_root_cached *tree = (struct rb_root_cached *)&sec->symbol_tree;
  162. struct symbol *sym = NULL, *tmp;
  163. __sym_for_each(tmp, tree, offset, offset) {
  164. if (tmp->len) {
  165. if (!sym) {
  166. sym = tmp;
  167. continue;
  168. }
  169. if (sym->offset != tmp->offset || sym->len != tmp->len) {
  170. /*
  171. * In the rare case of overlapping symbols,
  172. * pick the smaller one.
  173. *
  174. * TODO: outlaw overlapping symbols
  175. */
  176. if (tmp->len < sym->len)
  177. sym = tmp;
  178. }
  179. }
  180. }
  181. return sym ? sym->alias : NULL;
  182. }
  183. /*
  184. * Returns size of hole starting at @offset.
  185. */
  186. int find_symbol_hole_containing(const struct section *sec, unsigned long offset)
  187. {
  188. struct symbol_hole hole = {
  189. .key = offset,
  190. .sym = NULL,
  191. };
  192. struct rb_node *n;
  193. struct symbol *s;
  194. /*
  195. * Find the rightmost symbol for which @offset is after it.
  196. */
  197. n = rb_find(&hole, &sec->symbol_tree.rb_root, symbol_hole_by_offset);
  198. /* found a symbol that contains @offset */
  199. if (n)
  200. return 0; /* not a hole */
  201. /*
  202. * @offset >= sym->offset + sym->len, find symbol after it.
  203. * When hole.sym is empty, use the first node to compute the hole.
  204. * If there is no symbol in the section, the first node will be NULL,
  205. * in which case, -1 is returned to skip the whole section.
  206. */
  207. if (hole.sym)
  208. n = rb_next(&hole.sym->node);
  209. else
  210. n = rb_first_cached(&sec->symbol_tree);
  211. if (!n)
  212. return -1; /* until end of address space */
  213. /* hole until start of next symbol */
  214. s = rb_entry(n, struct symbol, node);
  215. return s->offset - offset;
  216. }
  217. struct symbol *find_func_containing(struct section *sec, unsigned long offset)
  218. {
  219. struct rb_root_cached *tree = (struct rb_root_cached *)&sec->symbol_tree;
  220. struct symbol *func;
  221. __sym_for_each(func, tree, offset, offset) {
  222. if (is_func_sym(func))
  223. return func->alias;
  224. }
  225. return NULL;
  226. }
  227. struct symbol *find_symbol_by_name(const struct elf *elf, const char *name)
  228. {
  229. struct symbol *sym;
  230. elf_hash_for_each_possible(symbol_name, sym, name_hash, str_hash(name)) {
  231. if (!strcmp(sym->name, name))
  232. return sym;
  233. }
  234. return NULL;
  235. }
  236. /* Find local symbol with matching STT_FILE */
  237. static struct symbol *find_local_symbol_by_file_and_name(const struct elf *elf,
  238. struct symbol *file,
  239. const char *name)
  240. {
  241. struct symbol *sym;
  242. elf_hash_for_each_possible(symbol_name, sym, name_hash, str_hash(name)) {
  243. if (sym->bind == STB_LOCAL && sym->file == file &&
  244. !strcmp(sym->name, name)) {
  245. return sym;
  246. }
  247. }
  248. return NULL;
  249. }
  250. struct symbol *find_global_symbol_by_name(const struct elf *elf, const char *name)
  251. {
  252. struct symbol *sym;
  253. elf_hash_for_each_possible(symbol_name, sym, name_hash, str_hash(name)) {
  254. if (!strcmp(sym->name, name) && !is_local_sym(sym))
  255. return sym;
  256. }
  257. return NULL;
  258. }
  259. struct reloc *find_reloc_by_dest_range(const struct elf *elf, struct section *sec,
  260. unsigned long offset, unsigned int len)
  261. {
  262. struct reloc *reloc, *r = NULL;
  263. struct section *rsec;
  264. unsigned long o;
  265. rsec = sec->rsec;
  266. if (!rsec)
  267. return NULL;
  268. for_offset_range(o, offset, offset + len) {
  269. elf_hash_for_each_possible(reloc, reloc, hash,
  270. sec_offset_hash(rsec, o)) {
  271. if (reloc->sec != rsec)
  272. continue;
  273. if (reloc_offset(reloc) >= offset &&
  274. reloc_offset(reloc) < offset + len) {
  275. if (!r || reloc_offset(reloc) < reloc_offset(r))
  276. r = reloc;
  277. }
  278. }
  279. if (r)
  280. return r;
  281. }
  282. return NULL;
  283. }
  284. struct reloc *find_reloc_by_dest(const struct elf *elf, struct section *sec, unsigned long offset)
  285. {
  286. return find_reloc_by_dest_range(elf, sec, offset, 1);
  287. }
  288. static bool is_dwarf_section(struct section *sec)
  289. {
  290. return !strncmp(sec->name, ".debug_", 7);
  291. }
  292. static int read_sections(struct elf *elf)
  293. {
  294. Elf_Scn *s = NULL;
  295. struct section *sec;
  296. size_t shstrndx, sections_nr;
  297. int i;
  298. if (elf_getshdrnum(elf->elf, &sections_nr)) {
  299. ERROR_ELF("elf_getshdrnum");
  300. return -1;
  301. }
  302. if (elf_getshdrstrndx(elf->elf, &shstrndx)) {
  303. ERROR_ELF("elf_getshdrstrndx");
  304. return -1;
  305. }
  306. if (!elf_alloc_hash(section, sections_nr) ||
  307. !elf_alloc_hash(section_name, sections_nr))
  308. return -1;
  309. elf->section_data = calloc(sections_nr, sizeof(*sec));
  310. if (!elf->section_data) {
  311. ERROR_GLIBC("calloc");
  312. return -1;
  313. }
  314. for (i = 0; i < sections_nr; i++) {
  315. sec = &elf->section_data[i];
  316. INIT_LIST_HEAD(&sec->symbol_list);
  317. s = elf_getscn(elf->elf, i);
  318. if (!s) {
  319. ERROR_ELF("elf_getscn");
  320. return -1;
  321. }
  322. sec->idx = elf_ndxscn(s);
  323. if (!gelf_getshdr(s, &sec->sh)) {
  324. ERROR_ELF("gelf_getshdr");
  325. return -1;
  326. }
  327. sec->name = elf_strptr(elf->elf, shstrndx, sec->sh.sh_name);
  328. if (!sec->name) {
  329. ERROR_ELF("elf_strptr");
  330. return -1;
  331. }
  332. if (sec_size(sec) != 0 && !is_dwarf_section(sec)) {
  333. sec->data = elf_getdata(s, NULL);
  334. if (!sec->data) {
  335. ERROR_ELF("elf_getdata");
  336. return -1;
  337. }
  338. if (sec->data->d_off != 0 ||
  339. sec->data->d_size != sec_size(sec)) {
  340. ERROR("unexpected data attributes for %s", sec->name);
  341. return -1;
  342. }
  343. }
  344. list_add_tail(&sec->list, &elf->sections);
  345. elf_hash_add(section, &sec->hash, sec->idx);
  346. elf_hash_add(section_name, &sec->name_hash, str_hash(sec->name));
  347. if (is_reloc_sec(sec))
  348. elf->num_relocs += sec_num_entries(sec);
  349. }
  350. if (opts.stats) {
  351. printf("nr_sections: %lu\n", (unsigned long)sections_nr);
  352. printf("section_bits: %d\n", elf->section_bits);
  353. }
  354. /* sanity check, one more call to elf_nextscn() should return NULL */
  355. if (elf_nextscn(elf->elf, s)) {
  356. ERROR("section entry mismatch");
  357. return -1;
  358. }
  359. return 0;
  360. }
  361. static const char *demangle_name(struct symbol *sym)
  362. {
  363. char *str;
  364. if (!is_local_sym(sym))
  365. return sym->name;
  366. if (!is_func_sym(sym) && !is_object_sym(sym))
  367. return sym->name;
  368. if (!strstarts(sym->name, "__UNIQUE_ID_") && !strchr(sym->name, '.'))
  369. return sym->name;
  370. str = strdup(sym->name);
  371. if (!str) {
  372. ERROR_GLIBC("strdup");
  373. return NULL;
  374. }
  375. for (int i = strlen(str) - 1; i >= 0; i--) {
  376. char c = str[i];
  377. if (!isdigit(c) && c != '.') {
  378. str[i + 1] = '\0';
  379. break;
  380. }
  381. }
  382. return str;
  383. }
  384. static int elf_add_symbol(struct elf *elf, struct symbol *sym)
  385. {
  386. struct list_head *entry;
  387. struct rb_node *pnode;
  388. struct symbol *iter;
  389. INIT_LIST_HEAD(&sym->pv_target);
  390. sym->alias = sym;
  391. sym->type = GELF_ST_TYPE(sym->sym.st_info);
  392. sym->bind = GELF_ST_BIND(sym->sym.st_info);
  393. if (is_file_sym(sym))
  394. elf->num_files++;
  395. sym->offset = sym->sym.st_value;
  396. sym->len = sym->sym.st_size;
  397. __sym_for_each(iter, &sym->sec->symbol_tree, sym->offset, sym->offset) {
  398. if (!is_undef_sym(iter) && iter->offset == sym->offset &&
  399. iter->type == sym->type && iter->len == sym->len)
  400. iter->alias = sym;
  401. }
  402. __sym_insert(sym, &sym->sec->symbol_tree);
  403. pnode = rb_prev(&sym->node);
  404. if (pnode)
  405. entry = &rb_entry(pnode, struct symbol, node)->list;
  406. else
  407. entry = &sym->sec->symbol_list;
  408. list_add(&sym->list, entry);
  409. list_add_tail(&sym->global_list, &elf->symbols);
  410. elf_hash_add(symbol, &sym->hash, sym->idx);
  411. elf_hash_add(symbol_name, &sym->name_hash, str_hash(sym->name));
  412. if (is_func_sym(sym) &&
  413. (strstarts(sym->name, "__pfx_") ||
  414. strstarts(sym->name, "__cfi_") ||
  415. strstarts(sym->name, "__pi___pfx_") ||
  416. strstarts(sym->name, "__pi___cfi_")))
  417. sym->prefix = 1;
  418. if (strstarts(sym->name, ".klp.sym"))
  419. sym->klp = 1;
  420. if (!sym->klp && !is_sec_sym(sym) && strstr(sym->name, ".cold")) {
  421. sym->cold = 1;
  422. /*
  423. * Clang doesn't mark cold subfunctions as STT_FUNC, which
  424. * breaks several objtool assumptions. Fake it.
  425. */
  426. sym->type = STT_FUNC;
  427. }
  428. sym->pfunc = sym->cfunc = sym;
  429. sym->demangled_name = demangle_name(sym);
  430. if (!sym->demangled_name)
  431. return -1;
  432. return 0;
  433. }
  434. static int read_symbols(struct elf *elf)
  435. {
  436. struct section *symtab, *symtab_shndx, *sec;
  437. struct symbol *sym, *pfunc, *file = NULL;
  438. int symbols_nr, i;
  439. char *coldstr;
  440. Elf_Data *shndx_data = NULL;
  441. Elf32_Word shndx;
  442. symtab = find_section_by_name(elf, ".symtab");
  443. if (symtab) {
  444. symtab_shndx = find_section_by_name(elf, ".symtab_shndx");
  445. if (symtab_shndx)
  446. shndx_data = symtab_shndx->data;
  447. symbols_nr = sec_num_entries(symtab);
  448. } else {
  449. /*
  450. * A missing symbol table is actually possible if it's an empty
  451. * .o file. This can happen for thunk_64.o. Make sure to at
  452. * least allocate the symbol hash tables so we can do symbol
  453. * lookups without crashing.
  454. */
  455. symbols_nr = 0;
  456. }
  457. if (!elf_alloc_hash(symbol, symbols_nr) ||
  458. !elf_alloc_hash(symbol_name, symbols_nr))
  459. return -1;
  460. elf->symbol_data = calloc(symbols_nr, sizeof(*sym));
  461. if (!elf->symbol_data) {
  462. ERROR_GLIBC("calloc");
  463. return -1;
  464. }
  465. INIT_LIST_HEAD(&elf->symbols);
  466. for (i = 0; i < symbols_nr; i++) {
  467. sym = &elf->symbol_data[i];
  468. sym->idx = i;
  469. if (!gelf_getsymshndx(symtab->data, shndx_data, i, &sym->sym,
  470. &shndx)) {
  471. ERROR_ELF("gelf_getsymshndx");
  472. return -1;
  473. }
  474. sym->name = elf_strptr(elf->elf, symtab->sh.sh_link,
  475. sym->sym.st_name);
  476. if (!sym->name) {
  477. ERROR_ELF("elf_strptr");
  478. return -1;
  479. }
  480. if ((sym->sym.st_shndx > SHN_UNDEF &&
  481. sym->sym.st_shndx < SHN_LORESERVE) ||
  482. (shndx_data && sym->sym.st_shndx == SHN_XINDEX)) {
  483. if (sym->sym.st_shndx != SHN_XINDEX)
  484. shndx = sym->sym.st_shndx;
  485. sym->sec = find_section_by_index(elf, shndx);
  486. if (!sym->sec) {
  487. ERROR("couldn't find section for symbol %s", sym->name);
  488. return -1;
  489. }
  490. if (GELF_ST_TYPE(sym->sym.st_info) == STT_SECTION) {
  491. sym->name = sym->sec->name;
  492. sym->sec->sym = sym;
  493. }
  494. } else
  495. sym->sec = find_section_by_index(elf, 0);
  496. if (elf_add_symbol(elf, sym))
  497. return -1;
  498. if (sym->type == STT_FILE)
  499. file = sym;
  500. else if (sym->bind == STB_LOCAL)
  501. sym->file = file;
  502. }
  503. if (opts.stats) {
  504. printf("nr_symbols: %lu\n", (unsigned long)symbols_nr);
  505. printf("symbol_bits: %d\n", elf->symbol_bits);
  506. }
  507. /* Create parent/child links for any cold subfunctions */
  508. list_for_each_entry(sec, &elf->sections, list) {
  509. sec_for_each_sym(sec, sym) {
  510. char *pname;
  511. size_t pnamelen;
  512. if (!sym->cold)
  513. continue;
  514. coldstr = strstr(sym->name, ".cold");
  515. if (!coldstr) {
  516. ERROR("%s(): cold subfunction without \".cold\"?", sym->name);
  517. return -1;
  518. }
  519. pnamelen = coldstr - sym->name;
  520. pname = strndup(sym->name, pnamelen);
  521. if (!pname) {
  522. ERROR("%s(): failed to allocate memory", sym->name);
  523. return -1;
  524. }
  525. pfunc = find_local_symbol_by_file_and_name(elf, sym->file, pname);
  526. if (!pfunc)
  527. pfunc = find_global_symbol_by_name(elf, pname);
  528. free(pname);
  529. if (!pfunc) {
  530. ERROR("%s(): can't find parent function", sym->name);
  531. return -1;
  532. }
  533. sym->pfunc = pfunc->alias;
  534. pfunc->cfunc = sym;
  535. pfunc->alias->cfunc = sym;
  536. /*
  537. * Unfortunately, -fnoreorder-functions puts the child
  538. * inside the parent. Remove the overlap so we can
  539. * have sane assumptions.
  540. *
  541. * Note that pfunc->len now no longer matches
  542. * pfunc->sym.st_size.
  543. */
  544. if (sym->sec == pfunc->sec &&
  545. sym->offset >= pfunc->offset &&
  546. sym->offset + sym->len == pfunc->offset + pfunc->len) {
  547. pfunc->len -= sym->len;
  548. }
  549. }
  550. }
  551. return 0;
  552. }
  553. static int mark_group_syms(struct elf *elf)
  554. {
  555. struct section *symtab, *sec;
  556. struct symbol *sym;
  557. symtab = find_section_by_name(elf, ".symtab");
  558. if (!symtab) {
  559. ERROR("no .symtab");
  560. return -1;
  561. }
  562. for_each_sec(elf, sec) {
  563. if (sec->sh.sh_type == SHT_GROUP &&
  564. sec->sh.sh_link == symtab->idx) {
  565. sym = find_symbol_by_index(elf, sec->sh.sh_info);
  566. if (!sym) {
  567. ERROR("%s: can't find SHT_GROUP signature symbol",
  568. sec->name);
  569. return -1;
  570. }
  571. sym->group_sec = sec;
  572. }
  573. }
  574. return 0;
  575. }
  576. /*
  577. * @sym's idx has changed. Update the relocs which reference it.
  578. */
  579. static int elf_update_sym_relocs(struct elf *elf, struct symbol *sym)
  580. {
  581. struct reloc *reloc;
  582. for (reloc = sym->relocs; reloc; reloc = sym_next_reloc(reloc))
  583. set_reloc_sym(elf, reloc, reloc->sym->idx);
  584. return 0;
  585. }
  586. /*
  587. * The libelf API is terrible; gelf_update_sym*() takes a data block relative
  588. * index value, *NOT* the symbol index. As such, iterate the data blocks and
  589. * adjust index until it fits.
  590. *
  591. * If no data block is found, allow adding a new data block provided the index
  592. * is only one past the end.
  593. */
  594. static int elf_update_symbol(struct elf *elf, struct section *symtab,
  595. struct section *symtab_shndx, struct symbol *sym)
  596. {
  597. Elf32_Word shndx;
  598. Elf_Data *symtab_data = NULL, *shndx_data = NULL;
  599. Elf64_Xword entsize = symtab->sh.sh_entsize;
  600. int max_idx, idx = sym->idx;
  601. Elf_Scn *s, *t = NULL;
  602. bool is_special_shndx = sym->sym.st_shndx >= SHN_LORESERVE &&
  603. sym->sym.st_shndx != SHN_XINDEX;
  604. shndx = is_special_shndx ? sym->sym.st_shndx : sym->sec->idx;
  605. s = elf_getscn(elf->elf, symtab->idx);
  606. if (!s) {
  607. ERROR_ELF("elf_getscn");
  608. return -1;
  609. }
  610. if (symtab_shndx) {
  611. t = elf_getscn(elf->elf, symtab_shndx->idx);
  612. if (!t) {
  613. ERROR_ELF("elf_getscn");
  614. return -1;
  615. }
  616. }
  617. for (;;) {
  618. /* get next data descriptor for the relevant sections */
  619. symtab_data = elf_getdata(s, symtab_data);
  620. if (t)
  621. shndx_data = elf_getdata(t, shndx_data);
  622. /* end-of-list */
  623. if (!symtab_data) {
  624. /*
  625. * Over-allocate to avoid O(n^2) symbol creation
  626. * behaviour. The down side is that libelf doesn't
  627. * like this; see elf_truncate_section() for the fixup.
  628. */
  629. int num = max(1U, sym->idx/3);
  630. void *buf;
  631. if (idx) {
  632. /* we don't do holes in symbol tables */
  633. ERROR("index out of range");
  634. return -1;
  635. }
  636. /* if @idx == 0, it's the next contiguous entry, create it */
  637. symtab_data = elf_newdata(s);
  638. if (t)
  639. shndx_data = elf_newdata(t);
  640. buf = calloc(num, entsize);
  641. if (!buf) {
  642. ERROR_GLIBC("calloc");
  643. return -1;
  644. }
  645. symtab_data->d_buf = buf;
  646. symtab_data->d_size = num * entsize;
  647. symtab_data->d_align = 1;
  648. symtab_data->d_type = ELF_T_SYM;
  649. mark_sec_changed(elf, symtab, true);
  650. symtab->truncate = true;
  651. if (t) {
  652. buf = calloc(num, sizeof(Elf32_Word));
  653. if (!buf) {
  654. ERROR_GLIBC("calloc");
  655. return -1;
  656. }
  657. shndx_data->d_buf = buf;
  658. shndx_data->d_size = num * sizeof(Elf32_Word);
  659. shndx_data->d_align = sizeof(Elf32_Word);
  660. shndx_data->d_type = ELF_T_WORD;
  661. mark_sec_changed(elf, symtab_shndx, true);
  662. symtab_shndx->truncate = true;
  663. }
  664. break;
  665. }
  666. /* empty blocks should not happen */
  667. if (!symtab_data->d_size) {
  668. ERROR("zero size data");
  669. return -1;
  670. }
  671. /* is this the right block? */
  672. max_idx = symtab_data->d_size / entsize;
  673. if (idx < max_idx)
  674. break;
  675. /* adjust index and try again */
  676. idx -= max_idx;
  677. }
  678. /* something went side-ways */
  679. if (idx < 0) {
  680. ERROR("negative index");
  681. return -1;
  682. }
  683. /* setup extended section index magic and write the symbol */
  684. if (shndx < SHN_LORESERVE || is_special_shndx) {
  685. sym->sym.st_shndx = shndx;
  686. if (!shndx_data)
  687. shndx = 0;
  688. } else {
  689. sym->sym.st_shndx = SHN_XINDEX;
  690. if (!shndx_data) {
  691. ERROR("no .symtab_shndx");
  692. return -1;
  693. }
  694. }
  695. if (!gelf_update_symshndx(symtab_data, shndx_data, idx, &sym->sym, shndx)) {
  696. ERROR_ELF("gelf_update_symshndx");
  697. return -1;
  698. }
  699. return 0;
  700. }
  701. struct symbol *elf_create_symbol(struct elf *elf, const char *name,
  702. struct section *sec, unsigned int bind,
  703. unsigned int type, unsigned long offset,
  704. size_t size)
  705. {
  706. struct section *symtab, *symtab_shndx;
  707. Elf32_Word first_non_local, new_idx;
  708. struct symbol *old, *sym;
  709. sym = calloc(1, sizeof(*sym));
  710. if (!sym) {
  711. ERROR_GLIBC("calloc");
  712. return NULL;
  713. }
  714. sym->name = strdup(name);
  715. if (!sym->name) {
  716. ERROR_GLIBC("strdup");
  717. return NULL;
  718. }
  719. if (type != STT_SECTION) {
  720. sym->sym.st_name = elf_add_string(elf, NULL, sym->name);
  721. if (sym->sym.st_name == -1)
  722. return NULL;
  723. }
  724. if (sec) {
  725. sym->sec = sec;
  726. } else {
  727. sym->sec = find_section_by_index(elf, 0);
  728. if (!sym->sec) {
  729. ERROR("no NULL section");
  730. return NULL;
  731. }
  732. }
  733. sym->sym.st_info = GELF_ST_INFO(bind, type);
  734. sym->sym.st_value = offset;
  735. sym->sym.st_size = size;
  736. symtab = find_section_by_name(elf, ".symtab");
  737. if (!symtab) {
  738. ERROR("no .symtab");
  739. return NULL;
  740. }
  741. symtab_shndx = find_section_by_name(elf, ".symtab_shndx");
  742. new_idx = sec_num_entries(symtab);
  743. if (bind != STB_LOCAL)
  744. goto non_local;
  745. /*
  746. * Move the first global symbol, as per sh_info, into a new, higher
  747. * symbol index. This frees up a spot for a new local symbol.
  748. */
  749. first_non_local = symtab->sh.sh_info;
  750. old = find_symbol_by_index(elf, first_non_local);
  751. if (old) {
  752. elf_hash_del(symbol, &old->hash, old->idx);
  753. elf_hash_add(symbol, &old->hash, new_idx);
  754. old->idx = new_idx;
  755. if (elf_update_symbol(elf, symtab, symtab_shndx, old)) {
  756. ERROR("elf_update_symbol move");
  757. return NULL;
  758. }
  759. if (elf_update_sym_relocs(elf, old))
  760. return NULL;
  761. if (old->group_sec) {
  762. old->group_sec->sh.sh_info = new_idx;
  763. mark_sec_changed(elf, old->group_sec, true);
  764. }
  765. new_idx = first_non_local;
  766. }
  767. /*
  768. * Either way, we will add a LOCAL symbol.
  769. */
  770. symtab->sh.sh_info += 1;
  771. non_local:
  772. sym->idx = new_idx;
  773. if (sym->idx && elf_update_symbol(elf, symtab, symtab_shndx, sym))
  774. return NULL;
  775. symtab->sh.sh_size += symtab->sh.sh_entsize;
  776. mark_sec_changed(elf, symtab, true);
  777. if (symtab_shndx) {
  778. symtab_shndx->sh.sh_size += sizeof(Elf32_Word);
  779. mark_sec_changed(elf, symtab_shndx, true);
  780. }
  781. if (elf_add_symbol(elf, sym))
  782. return NULL;
  783. return sym;
  784. }
  785. struct symbol *elf_create_section_symbol(struct elf *elf, struct section *sec)
  786. {
  787. struct symbol *sym = calloc(1, sizeof(*sym));
  788. sym = elf_create_symbol(elf, sec->name, sec, STB_LOCAL, STT_SECTION, 0, 0);
  789. if (!sym)
  790. return NULL;
  791. sec->sym = sym;
  792. return sym;
  793. }
  794. struct reloc *elf_init_reloc(struct elf *elf, struct section *rsec,
  795. unsigned int reloc_idx, unsigned long offset,
  796. struct symbol *sym, s64 addend, unsigned int type)
  797. {
  798. struct reloc *reloc, empty = { 0 };
  799. if (reloc_idx >= sec_num_entries(rsec)) {
  800. ERROR("%s: bad reloc_idx %u for %s with %d relocs",
  801. __func__, reloc_idx, rsec->name, sec_num_entries(rsec));
  802. return NULL;
  803. }
  804. reloc = &rsec->relocs[reloc_idx];
  805. if (memcmp(reloc, &empty, sizeof(empty))) {
  806. ERROR("%s: %s: reloc %d already initialized!",
  807. __func__, rsec->name, reloc_idx);
  808. return NULL;
  809. }
  810. reloc->sec = rsec;
  811. reloc->sym = sym;
  812. set_reloc_offset(elf, reloc, offset);
  813. set_reloc_sym(elf, reloc, sym->idx);
  814. set_reloc_type(elf, reloc, type);
  815. set_reloc_addend(elf, reloc, addend);
  816. elf_hash_add(reloc, &reloc->hash, reloc_hash(reloc));
  817. set_sym_next_reloc(reloc, sym->relocs);
  818. sym->relocs = reloc;
  819. return reloc;
  820. }
  821. struct reloc *elf_init_reloc_text_sym(struct elf *elf, struct section *sec,
  822. unsigned long offset,
  823. unsigned int reloc_idx,
  824. struct section *insn_sec,
  825. unsigned long insn_off)
  826. {
  827. struct symbol *sym = insn_sec->sym;
  828. s64 addend = insn_off;
  829. if (!is_text_sec(insn_sec)) {
  830. ERROR("bad call to %s() for data symbol %s", __func__, sym->name);
  831. return NULL;
  832. }
  833. if (!sym) {
  834. /*
  835. * Due to how weak functions work, we must use section based
  836. * relocations. Symbol based relocations would result in the
  837. * weak and non-weak function annotations being overlaid on the
  838. * non-weak function after linking.
  839. */
  840. sym = elf_create_section_symbol(elf, insn_sec);
  841. if (!sym)
  842. return NULL;
  843. }
  844. return elf_init_reloc(elf, sec->rsec, reloc_idx, offset, sym, addend,
  845. elf_text_rela_type(elf));
  846. }
  847. struct reloc *elf_init_reloc_data_sym(struct elf *elf, struct section *sec,
  848. unsigned long offset,
  849. unsigned int reloc_idx,
  850. struct symbol *sym,
  851. s64 addend)
  852. {
  853. if (is_text_sec(sec)) {
  854. ERROR("bad call to %s() for text symbol %s", __func__, sym->name);
  855. return NULL;
  856. }
  857. return elf_init_reloc(elf, sec->rsec, reloc_idx, offset, sym, addend,
  858. elf_data_rela_type(elf));
  859. }
  860. static int read_relocs(struct elf *elf)
  861. {
  862. unsigned long nr_reloc, max_reloc = 0;
  863. struct section *rsec;
  864. struct reloc *reloc;
  865. unsigned int symndx;
  866. struct symbol *sym;
  867. int i;
  868. if (!elf_alloc_hash(reloc, elf->num_relocs))
  869. return -1;
  870. list_for_each_entry(rsec, &elf->sections, list) {
  871. if (!is_reloc_sec(rsec))
  872. continue;
  873. rsec->base = find_section_by_index(elf, rsec->sh.sh_info);
  874. if (!rsec->base) {
  875. ERROR("can't find base section for reloc section %s", rsec->name);
  876. return -1;
  877. }
  878. rsec->base->rsec = rsec;
  879. /* nr_alloc_relocs=0: libelf owns d_buf */
  880. rsec->nr_alloc_relocs = 0;
  881. rsec->relocs = calloc(sec_num_entries(rsec), sizeof(*reloc));
  882. if (!rsec->relocs) {
  883. ERROR_GLIBC("calloc");
  884. return -1;
  885. }
  886. nr_reloc = 0;
  887. for (i = 0; i < sec_num_entries(rsec); i++) {
  888. reloc = &rsec->relocs[i];
  889. reloc->sec = rsec;
  890. symndx = reloc_sym(reloc);
  891. reloc->sym = sym = find_symbol_by_index(elf, symndx);
  892. if (!reloc->sym) {
  893. ERROR("can't find reloc entry symbol %d for %s", symndx, rsec->name);
  894. return -1;
  895. }
  896. elf_hash_add(reloc, &reloc->hash, reloc_hash(reloc));
  897. set_sym_next_reloc(reloc, sym->relocs);
  898. sym->relocs = reloc;
  899. nr_reloc++;
  900. }
  901. max_reloc = max(max_reloc, nr_reloc);
  902. }
  903. if (opts.stats) {
  904. printf("max_reloc: %lu\n", max_reloc);
  905. printf("num_relocs: %lu\n", elf->num_relocs);
  906. printf("reloc_bits: %d\n", elf->reloc_bits);
  907. }
  908. return 0;
  909. }
  910. struct elf *elf_open_read(const char *name, int flags)
  911. {
  912. struct elf *elf;
  913. Elf_Cmd cmd;
  914. elf_version(EV_CURRENT);
  915. elf = malloc(sizeof(*elf));
  916. if (!elf) {
  917. ERROR_GLIBC("malloc");
  918. return NULL;
  919. }
  920. memset(elf, 0, sizeof(*elf));
  921. INIT_LIST_HEAD(&elf->sections);
  922. elf->fd = open(name, flags);
  923. if (elf->fd == -1) {
  924. fprintf(stderr, "objtool: Can't open '%s': %s\n",
  925. name, strerror(errno));
  926. goto err;
  927. }
  928. elf->name = strdup(name);
  929. if (!elf->name) {
  930. ERROR_GLIBC("strdup");
  931. return NULL;
  932. }
  933. if ((flags & O_ACCMODE) == O_RDONLY)
  934. cmd = ELF_C_READ_MMAP;
  935. else if ((flags & O_ACCMODE) == O_RDWR)
  936. cmd = ELF_C_RDWR;
  937. else /* O_WRONLY */
  938. cmd = ELF_C_WRITE;
  939. elf->elf = elf_begin(elf->fd, cmd, NULL);
  940. if (!elf->elf) {
  941. ERROR_ELF("elf_begin");
  942. goto err;
  943. }
  944. if (!gelf_getehdr(elf->elf, &elf->ehdr)) {
  945. ERROR_ELF("gelf_getehdr");
  946. goto err;
  947. }
  948. if (read_sections(elf))
  949. goto err;
  950. if (read_symbols(elf))
  951. goto err;
  952. if (mark_group_syms(elf))
  953. goto err;
  954. if (read_relocs(elf))
  955. goto err;
  956. return elf;
  957. err:
  958. elf_close(elf);
  959. return NULL;
  960. }
  961. struct elf *elf_create_file(GElf_Ehdr *ehdr, const char *name)
  962. {
  963. struct section *null, *symtab, *strtab, *shstrtab;
  964. char *tmp_name;
  965. struct symbol *sym;
  966. struct elf *elf;
  967. elf_version(EV_CURRENT);
  968. elf = calloc(1, sizeof(*elf));
  969. if (!elf) {
  970. ERROR_GLIBC("calloc");
  971. return NULL;
  972. }
  973. INIT_LIST_HEAD(&elf->sections);
  974. tmp_name = malloc(strlen(name) + 8);
  975. if (!tmp_name) {
  976. ERROR_GLIBC("malloc");
  977. return NULL;
  978. }
  979. sprintf(tmp_name, "%s.XXXXXX", name);
  980. elf->fd = mkstemp(tmp_name);
  981. if (elf->fd == -1) {
  982. ERROR_GLIBC("can't create tmp file");
  983. exit(1);
  984. }
  985. elf->tmp_name = tmp_name;
  986. elf->name = strdup(name);
  987. if (!elf->name) {
  988. ERROR_GLIBC("strdup");
  989. return NULL;
  990. }
  991. elf->elf = elf_begin(elf->fd, ELF_C_WRITE, NULL);
  992. if (!elf->elf) {
  993. ERROR_ELF("elf_begin");
  994. return NULL;
  995. }
  996. if (!gelf_newehdr(elf->elf, ELFCLASS64)) {
  997. ERROR_ELF("gelf_newehdr");
  998. return NULL;
  999. }
  1000. memcpy(&elf->ehdr, ehdr, sizeof(elf->ehdr));
  1001. if (!gelf_update_ehdr(elf->elf, &elf->ehdr)) {
  1002. ERROR_ELF("gelf_update_ehdr");
  1003. return NULL;
  1004. }
  1005. INIT_LIST_HEAD(&elf->symbols);
  1006. if (!elf_alloc_hash(section, 1000) ||
  1007. !elf_alloc_hash(section_name, 1000) ||
  1008. !elf_alloc_hash(symbol, 10000) ||
  1009. !elf_alloc_hash(symbol_name, 10000) ||
  1010. !elf_alloc_hash(reloc, 100000))
  1011. return NULL;
  1012. null = elf_create_section(elf, NULL, 0, 0, SHT_NULL, 0, 0);
  1013. shstrtab = elf_create_section(elf, NULL, 0, 0, SHT_STRTAB, 1, 0);
  1014. strtab = elf_create_section(elf, NULL, 0, 0, SHT_STRTAB, 1, 0);
  1015. if (!null || !shstrtab || !strtab)
  1016. return NULL;
  1017. null->name = "";
  1018. shstrtab->name = ".shstrtab";
  1019. strtab->name = ".strtab";
  1020. null->sh.sh_name = elf_add_string(elf, shstrtab, null->name);
  1021. shstrtab->sh.sh_name = elf_add_string(elf, shstrtab, shstrtab->name);
  1022. strtab->sh.sh_name = elf_add_string(elf, shstrtab, strtab->name);
  1023. if (null->sh.sh_name == -1 || shstrtab->sh.sh_name == -1 || strtab->sh.sh_name == -1)
  1024. return NULL;
  1025. elf_hash_add(section_name, &null->name_hash, str_hash(null->name));
  1026. elf_hash_add(section_name, &strtab->name_hash, str_hash(strtab->name));
  1027. elf_hash_add(section_name, &shstrtab->name_hash, str_hash(shstrtab->name));
  1028. if (elf_add_string(elf, strtab, "") == -1)
  1029. return NULL;
  1030. symtab = elf_create_section(elf, ".symtab", 0x18, 0x18, SHT_SYMTAB, 0x8, 0);
  1031. if (!symtab)
  1032. return NULL;
  1033. symtab->sh.sh_link = strtab->idx;
  1034. symtab->sh.sh_info = 1;
  1035. elf->ehdr.e_shstrndx = shstrtab->idx;
  1036. if (!gelf_update_ehdr(elf->elf, &elf->ehdr)) {
  1037. ERROR_ELF("gelf_update_ehdr");
  1038. return NULL;
  1039. }
  1040. sym = calloc(1, sizeof(*sym));
  1041. if (!sym) {
  1042. ERROR_GLIBC("calloc");
  1043. return NULL;
  1044. }
  1045. sym->name = "";
  1046. sym->sec = null;
  1047. elf_add_symbol(elf, sym);
  1048. return elf;
  1049. }
  1050. unsigned int elf_add_string(struct elf *elf, struct section *strtab, const char *str)
  1051. {
  1052. unsigned int offset;
  1053. if (!strtab)
  1054. strtab = find_section_by_name(elf, ".strtab");
  1055. if (!strtab) {
  1056. ERROR("can't find .strtab section");
  1057. return -1;
  1058. }
  1059. if (!strtab->sh.sh_addralign) {
  1060. ERROR("'%s': invalid sh_addralign", strtab->name);
  1061. return -1;
  1062. }
  1063. offset = ALIGN(strtab->sh.sh_size, strtab->sh.sh_addralign);
  1064. if (!elf_add_data(elf, strtab, str, strlen(str) + 1))
  1065. return -1;
  1066. return offset;
  1067. }
  1068. void *elf_add_data(struct elf *elf, struct section *sec, const void *data, size_t size)
  1069. {
  1070. unsigned long offset;
  1071. Elf_Scn *s;
  1072. if (!sec->sh.sh_addralign) {
  1073. ERROR("'%s': invalid sh_addralign", sec->name);
  1074. return NULL;
  1075. }
  1076. s = elf_getscn(elf->elf, sec->idx);
  1077. if (!s) {
  1078. ERROR_ELF("elf_getscn");
  1079. return NULL;
  1080. }
  1081. sec->data = elf_newdata(s);
  1082. if (!sec->data) {
  1083. ERROR_ELF("elf_newdata");
  1084. return NULL;
  1085. }
  1086. sec->data->d_buf = calloc(1, size);
  1087. if (!sec->data->d_buf) {
  1088. ERROR_GLIBC("calloc");
  1089. return NULL;
  1090. }
  1091. if (data)
  1092. memcpy(sec->data->d_buf, data, size);
  1093. sec->data->d_size = size;
  1094. sec->data->d_align = sec->sh.sh_addralign;
  1095. offset = ALIGN(sec->sh.sh_size, sec->sh.sh_addralign);
  1096. sec->sh.sh_size = offset + size;
  1097. mark_sec_changed(elf, sec, true);
  1098. return sec->data->d_buf;
  1099. }
  1100. struct section *elf_create_section(struct elf *elf, const char *name,
  1101. size_t size, size_t entsize,
  1102. unsigned int type, unsigned int align,
  1103. unsigned int flags)
  1104. {
  1105. struct section *sec, *shstrtab;
  1106. Elf_Scn *s;
  1107. if (name && find_section_by_name(elf, name)) {
  1108. ERROR("section '%s' already exists", name);
  1109. return NULL;
  1110. }
  1111. sec = calloc(1, sizeof(*sec));
  1112. if (!sec) {
  1113. ERROR_GLIBC("calloc");
  1114. return NULL;
  1115. }
  1116. INIT_LIST_HEAD(&sec->symbol_list);
  1117. /* don't actually create the section, just the data structures */
  1118. if (type == SHT_NULL)
  1119. goto add;
  1120. s = elf_newscn(elf->elf);
  1121. if (!s) {
  1122. ERROR_ELF("elf_newscn");
  1123. return NULL;
  1124. }
  1125. sec->idx = elf_ndxscn(s);
  1126. if (size) {
  1127. sec->data = elf_newdata(s);
  1128. if (!sec->data) {
  1129. ERROR_ELF("elf_newdata");
  1130. return NULL;
  1131. }
  1132. sec->data->d_size = size;
  1133. sec->data->d_align = 1;
  1134. sec->data->d_buf = calloc(1, size);
  1135. if (!sec->data->d_buf) {
  1136. ERROR_GLIBC("calloc");
  1137. return NULL;
  1138. }
  1139. }
  1140. if (!gelf_getshdr(s, &sec->sh)) {
  1141. ERROR_ELF("gelf_getshdr");
  1142. return NULL;
  1143. }
  1144. sec->sh.sh_size = size;
  1145. sec->sh.sh_entsize = entsize;
  1146. sec->sh.sh_type = type;
  1147. sec->sh.sh_addralign = align;
  1148. sec->sh.sh_flags = flags;
  1149. if (name) {
  1150. sec->name = strdup(name);
  1151. if (!sec->name) {
  1152. ERROR("strdup");
  1153. return NULL;
  1154. }
  1155. /* Add section name to .shstrtab (or .strtab for Clang) */
  1156. shstrtab = find_section_by_name(elf, ".shstrtab");
  1157. if (!shstrtab) {
  1158. shstrtab = find_section_by_name(elf, ".strtab");
  1159. if (!shstrtab) {
  1160. ERROR("can't find .shstrtab or .strtab");
  1161. return NULL;
  1162. }
  1163. }
  1164. sec->sh.sh_name = elf_add_string(elf, shstrtab, sec->name);
  1165. if (sec->sh.sh_name == -1)
  1166. return NULL;
  1167. elf_hash_add(section_name, &sec->name_hash, str_hash(sec->name));
  1168. }
  1169. add:
  1170. list_add_tail(&sec->list, &elf->sections);
  1171. elf_hash_add(section, &sec->hash, sec->idx);
  1172. mark_sec_changed(elf, sec, true);
  1173. return sec;
  1174. }
  1175. static int elf_alloc_reloc(struct elf *elf, struct section *rsec)
  1176. {
  1177. struct reloc *old_relocs, *old_relocs_end, *new_relocs;
  1178. unsigned int nr_relocs_old = sec_num_entries(rsec);
  1179. unsigned int nr_relocs_new = nr_relocs_old + 1;
  1180. unsigned long nr_alloc;
  1181. struct symbol *sym;
  1182. if (!rsec->data) {
  1183. rsec->data = elf_newdata(elf_getscn(elf->elf, rsec->idx));
  1184. if (!rsec->data) {
  1185. ERROR_ELF("elf_newdata");
  1186. return -1;
  1187. }
  1188. rsec->data->d_align = 1;
  1189. rsec->data->d_type = ELF_T_RELA;
  1190. rsec->data->d_buf = NULL;
  1191. }
  1192. rsec->data->d_size = nr_relocs_new * elf_rela_size(elf);
  1193. rsec->sh.sh_size = rsec->data->d_size;
  1194. nr_alloc = max(64UL, roundup_pow_of_two(nr_relocs_new));
  1195. if (nr_alloc <= rsec->nr_alloc_relocs)
  1196. return 0;
  1197. if (rsec->data->d_buf && !rsec->nr_alloc_relocs) {
  1198. void *orig_buf = rsec->data->d_buf;
  1199. /*
  1200. * The original d_buf is owned by libelf so it can't be
  1201. * realloced.
  1202. */
  1203. rsec->data->d_buf = malloc(nr_alloc * elf_rela_size(elf));
  1204. if (!rsec->data->d_buf) {
  1205. ERROR_GLIBC("malloc");
  1206. return -1;
  1207. }
  1208. memcpy(rsec->data->d_buf, orig_buf,
  1209. nr_relocs_old * elf_rela_size(elf));
  1210. } else {
  1211. rsec->data->d_buf = realloc(rsec->data->d_buf,
  1212. nr_alloc * elf_rela_size(elf));
  1213. if (!rsec->data->d_buf) {
  1214. ERROR_GLIBC("realloc");
  1215. return -1;
  1216. }
  1217. }
  1218. rsec->nr_alloc_relocs = nr_alloc;
  1219. old_relocs = rsec->relocs;
  1220. new_relocs = calloc(nr_alloc, sizeof(struct reloc));
  1221. if (!new_relocs) {
  1222. ERROR_GLIBC("calloc");
  1223. return -1;
  1224. }
  1225. if (!old_relocs)
  1226. goto done;
  1227. /*
  1228. * The struct reloc's address has changed. Update all the symbols and
  1229. * relocs which reference it.
  1230. */
  1231. old_relocs_end = &old_relocs[nr_relocs_old];
  1232. for_each_sym(elf, sym) {
  1233. struct reloc *reloc;
  1234. reloc = sym->relocs;
  1235. if (!reloc)
  1236. continue;
  1237. if (reloc >= old_relocs && reloc < old_relocs_end)
  1238. sym->relocs = &new_relocs[reloc - old_relocs];
  1239. while (1) {
  1240. struct reloc *next_reloc = sym_next_reloc(reloc);
  1241. if (!next_reloc)
  1242. break;
  1243. if (next_reloc >= old_relocs && next_reloc < old_relocs_end)
  1244. set_sym_next_reloc(reloc, &new_relocs[next_reloc - old_relocs]);
  1245. reloc = next_reloc;
  1246. }
  1247. }
  1248. memcpy(new_relocs, old_relocs, nr_relocs_old * sizeof(struct reloc));
  1249. for (int i = 0; i < nr_relocs_old; i++) {
  1250. struct reloc *old = &old_relocs[i];
  1251. struct reloc *new = &new_relocs[i];
  1252. u32 key = reloc_hash(old);
  1253. elf_hash_del(reloc, &old->hash, key);
  1254. elf_hash_add(reloc, &new->hash, key);
  1255. }
  1256. free(old_relocs);
  1257. done:
  1258. rsec->relocs = new_relocs;
  1259. return 0;
  1260. }
  1261. struct section *elf_create_rela_section(struct elf *elf, struct section *sec,
  1262. unsigned int nr_relocs)
  1263. {
  1264. struct section *rsec;
  1265. char *rsec_name;
  1266. rsec_name = malloc(strlen(sec->name) + strlen(".rela") + 1);
  1267. if (!rsec_name) {
  1268. ERROR_GLIBC("malloc");
  1269. return NULL;
  1270. }
  1271. strcpy(rsec_name, ".rela");
  1272. strcat(rsec_name, sec->name);
  1273. rsec = elf_create_section(elf, rsec_name, nr_relocs * elf_rela_size(elf),
  1274. elf_rela_size(elf), SHT_RELA, elf_addr_size(elf),
  1275. SHF_INFO_LINK);
  1276. free(rsec_name);
  1277. if (!rsec)
  1278. return NULL;
  1279. if (nr_relocs) {
  1280. rsec->data->d_type = ELF_T_RELA;
  1281. rsec->nr_alloc_relocs = nr_relocs;
  1282. rsec->relocs = calloc(nr_relocs, sizeof(struct reloc));
  1283. if (!rsec->relocs) {
  1284. ERROR_GLIBC("calloc");
  1285. return NULL;
  1286. }
  1287. }
  1288. rsec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx;
  1289. rsec->sh.sh_info = sec->idx;
  1290. sec->rsec = rsec;
  1291. rsec->base = sec;
  1292. return rsec;
  1293. }
  1294. struct reloc *elf_create_reloc(struct elf *elf, struct section *sec,
  1295. unsigned long offset,
  1296. struct symbol *sym, s64 addend,
  1297. unsigned int type)
  1298. {
  1299. struct section *rsec = sec->rsec;
  1300. if (!rsec) {
  1301. rsec = elf_create_rela_section(elf, sec, 0);
  1302. if (!rsec)
  1303. return NULL;
  1304. }
  1305. if (find_reloc_by_dest(elf, sec, offset)) {
  1306. ERROR_FUNC(sec, offset, "duplicate reloc");
  1307. return NULL;
  1308. }
  1309. if (elf_alloc_reloc(elf, rsec))
  1310. return NULL;
  1311. mark_sec_changed(elf, rsec, true);
  1312. return elf_init_reloc(elf, rsec, sec_num_entries(rsec) - 1, offset, sym,
  1313. addend, type);
  1314. }
  1315. struct section *elf_create_section_pair(struct elf *elf, const char *name,
  1316. size_t entsize, unsigned int nr,
  1317. unsigned int nr_relocs)
  1318. {
  1319. struct section *sec;
  1320. sec = elf_create_section(elf, name, nr * entsize, entsize,
  1321. SHT_PROGBITS, 1, SHF_ALLOC);
  1322. if (!sec)
  1323. return NULL;
  1324. if (!elf_create_rela_section(elf, sec, nr_relocs))
  1325. return NULL;
  1326. return sec;
  1327. }
  1328. int elf_write_insn(struct elf *elf, struct section *sec,
  1329. unsigned long offset, unsigned int len,
  1330. const char *insn)
  1331. {
  1332. Elf_Data *data = sec->data;
  1333. if (data->d_type != ELF_T_BYTE || data->d_off) {
  1334. ERROR("write to unexpected data for section: %s", sec->name);
  1335. return -1;
  1336. }
  1337. memcpy(data->d_buf + offset, insn, len);
  1338. mark_sec_changed(elf, sec, true);
  1339. return 0;
  1340. }
  1341. /*
  1342. * When Elf_Scn::sh_size is smaller than the combined Elf_Data::d_size
  1343. * do you:
  1344. *
  1345. * A) adhere to the section header and truncate the data, or
  1346. * B) ignore the section header and write out all the data you've got?
  1347. *
  1348. * Yes, libelf sucks and we need to manually truncate if we over-allocate data.
  1349. */
  1350. static int elf_truncate_section(struct elf *elf, struct section *sec)
  1351. {
  1352. u64 size = sec_size(sec);
  1353. bool truncated = false;
  1354. Elf_Data *data = NULL;
  1355. Elf_Scn *s;
  1356. s = elf_getscn(elf->elf, sec->idx);
  1357. if (!s) {
  1358. ERROR_ELF("elf_getscn");
  1359. return -1;
  1360. }
  1361. for (;;) {
  1362. /* get next data descriptor for the relevant section */
  1363. data = elf_getdata(s, data);
  1364. if (!data) {
  1365. if (size) {
  1366. ERROR("end of section data but non-zero size left\n");
  1367. return -1;
  1368. }
  1369. return 0;
  1370. }
  1371. if (truncated) {
  1372. /* when we remove symbols */
  1373. ERROR("truncated; but more data\n");
  1374. return -1;
  1375. }
  1376. if (!data->d_size) {
  1377. ERROR("zero size data");
  1378. return -1;
  1379. }
  1380. if (data->d_size > size) {
  1381. truncated = true;
  1382. data->d_size = size;
  1383. }
  1384. size -= data->d_size;
  1385. }
  1386. }
  1387. int elf_write(struct elf *elf)
  1388. {
  1389. struct section *sec;
  1390. Elf_Scn *s;
  1391. /* Update changed relocation sections and section headers: */
  1392. list_for_each_entry(sec, &elf->sections, list) {
  1393. if (sec->truncate && elf_truncate_section(elf, sec))
  1394. return -1;
  1395. if (sec_changed(sec)) {
  1396. s = elf_getscn(elf->elf, sec->idx);
  1397. if (!s) {
  1398. ERROR_ELF("elf_getscn");
  1399. return -1;
  1400. }
  1401. /* Note this also flags the section dirty */
  1402. if (!gelf_update_shdr(s, &sec->sh)) {
  1403. ERROR_ELF("gelf_update_shdr");
  1404. return -1;
  1405. }
  1406. mark_sec_changed(elf, sec, false);
  1407. }
  1408. }
  1409. /* Make sure the new section header entries get updated properly. */
  1410. elf_flagelf(elf->elf, ELF_C_SET, ELF_F_DIRTY);
  1411. /* Write all changes to the file. */
  1412. if (elf_update(elf->elf, ELF_C_WRITE) < 0) {
  1413. ERROR_ELF("elf_update");
  1414. return -1;
  1415. }
  1416. elf->changed = false;
  1417. return 0;
  1418. }
  1419. int elf_close(struct elf *elf)
  1420. {
  1421. if (elf->elf)
  1422. elf_end(elf->elf);
  1423. if (elf->fd > 0)
  1424. close(elf->fd);
  1425. if (elf->tmp_name && rename(elf->tmp_name, elf->name))
  1426. return -1;
  1427. /*
  1428. * NOTE: All remaining allocations are leaked on purpose. Objtool is
  1429. * about to exit anyway.
  1430. */
  1431. return 0;
  1432. }