target-reloc.h 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  1. // target-reloc.h -- target specific relocation support -*- C++ -*-
  2. // Copyright (C) 2006-2022 Free Software Foundation, Inc.
  3. // Written by Ian Lance Taylor <iant@google.com>.
  4. // This file is part of gold.
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation; either version 3 of the License, or
  8. // (at your option) any later version.
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. // MA 02110-1301, USA.
  17. #ifndef GOLD_TARGET_RELOC_H
  18. #define GOLD_TARGET_RELOC_H
  19. #include "elfcpp.h"
  20. #include "symtab.h"
  21. #include "object.h"
  22. #include "reloc.h"
  23. #include "reloc-types.h"
  24. namespace gold
  25. {
  26. // This function implements the generic part of reloc scanning. The
  27. // template parameter Scan must be a class type which provides two
  28. // functions: local() and global(). Those functions implement the
  29. // machine specific part of scanning. We do it this way to
  30. // avoid making a function call for each relocation, and to avoid
  31. // repeating the generic code for each target.
  32. template<int size, bool big_endian, typename Target_type,
  33. typename Scan, typename Classify_reloc>
  34. inline void
  35. scan_relocs(
  36. Symbol_table* symtab,
  37. Layout* layout,
  38. Target_type* target,
  39. Sized_relobj_file<size, big_endian>* object,
  40. unsigned int data_shndx,
  41. const unsigned char* prelocs,
  42. size_t reloc_count,
  43. Output_section* output_section,
  44. bool needs_special_offset_handling,
  45. size_t local_count,
  46. const unsigned char* plocal_syms)
  47. {
  48. typedef typename Classify_reloc::Reltype Reltype;
  49. const int reloc_size = Classify_reloc::reloc_size;
  50. const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
  51. Scan scan;
  52. for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
  53. {
  54. Reltype reloc(prelocs);
  55. if (needs_special_offset_handling
  56. && !output_section->is_input_address_mapped(object, data_shndx,
  57. reloc.get_r_offset()))
  58. continue;
  59. unsigned int r_sym = Classify_reloc::get_r_sym(&reloc);
  60. unsigned int r_type = Classify_reloc::get_r_type(&reloc);
  61. if (r_sym < local_count)
  62. {
  63. gold_assert(plocal_syms != NULL);
  64. typename elfcpp::Sym<size, big_endian> lsym(plocal_syms
  65. + r_sym * sym_size);
  66. unsigned int shndx = lsym.get_st_shndx();
  67. bool is_ordinary;
  68. shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
  69. // If RELOC is a relocation against a local symbol in a
  70. // section we are discarding then we can ignore it. It will
  71. // eventually become a reloc against the value zero.
  72. //
  73. // FIXME: We should issue a warning if this is an
  74. // allocated section; is this the best place to do it?
  75. //
  76. // FIXME: The old GNU linker would in some cases look
  77. // for the linkonce section which caused this section to
  78. // be discarded, and, if the other section was the same
  79. // size, change the reloc to refer to the other section.
  80. // That seems risky and weird to me, and I don't know of
  81. // any case where it is actually required.
  82. bool is_discarded = (is_ordinary
  83. && shndx != elfcpp::SHN_UNDEF
  84. && !object->is_section_included(shndx)
  85. && !symtab->is_section_folded(object, shndx));
  86. scan.local(symtab, layout, target, object, data_shndx,
  87. output_section, reloc, r_type, lsym, is_discarded);
  88. }
  89. else
  90. {
  91. Symbol* gsym = object->global_symbol(r_sym);
  92. gold_assert(gsym != NULL);
  93. if (gsym->is_forwarder())
  94. gsym = symtab->resolve_forwards(gsym);
  95. scan.global(symtab, layout, target, object, data_shndx,
  96. output_section, reloc, r_type, gsym);
  97. }
  98. }
  99. }
  100. // Behavior for relocations to discarded comdat sections.
  101. enum Comdat_behavior
  102. {
  103. CB_UNDETERMINED, // Not yet determined -- need to look at section name.
  104. CB_PRETEND, // Attempt to map to the corresponding kept section.
  105. CB_IGNORE, // Ignore the relocation.
  106. CB_ERROR // Print an error.
  107. };
  108. class Default_comdat_behavior
  109. {
  110. public:
  111. // Decide what the linker should do for relocations that refer to
  112. // discarded comdat sections. This decision is based on the name of
  113. // the section being relocated.
  114. inline Comdat_behavior
  115. get(const char* name)
  116. {
  117. if (Layout::is_debug_info_section(name))
  118. return CB_PRETEND;
  119. if (strcmp(name, ".eh_frame") == 0
  120. || is_prefix_of (".gnu.build.attributes", name)
  121. || strcmp(name, ".gcc_except_table") == 0)
  122. return CB_IGNORE;
  123. return CB_ERROR;
  124. }
  125. };
  126. // Give an error for a symbol with non-default visibility which is not
  127. // defined locally.
  128. inline void
  129. visibility_error(const Symbol* sym)
  130. {
  131. const char* v;
  132. switch (sym->visibility())
  133. {
  134. case elfcpp::STV_INTERNAL:
  135. v = _("internal");
  136. break;
  137. case elfcpp::STV_HIDDEN:
  138. v = _("hidden");
  139. break;
  140. case elfcpp::STV_PROTECTED:
  141. v = _("protected");
  142. break;
  143. default:
  144. gold_unreachable();
  145. }
  146. gold_error(_("%s symbol '%s' is not defined locally"),
  147. v, sym->name());
  148. }
  149. // Return true if we are should issue an error saying that SYM is an
  150. // undefined symbol. This is called if there is a relocation against
  151. // SYM.
  152. inline bool
  153. issue_undefined_symbol_error(const Symbol* sym)
  154. {
  155. // We only report global symbols.
  156. if (sym == NULL)
  157. return false;
  158. // We only report undefined symbols.
  159. if (!sym->is_undefined() && !sym->is_placeholder())
  160. return false;
  161. // We don't report weak symbols.
  162. if (sym->is_weak_undefined())
  163. return false;
  164. // We don't report symbols defined in discarded sections,
  165. // unless they're placeholder symbols that should have been
  166. // provided by a plugin.
  167. if (sym->is_defined_in_discarded_section() && !sym->is_placeholder())
  168. return false;
  169. // If the target defines this symbol, don't report it here.
  170. if (parameters->target().is_defined_by_abi(sym))
  171. return false;
  172. // See if we've been told to ignore whether this symbol is
  173. // undefined.
  174. const char* const u = parameters->options().unresolved_symbols();
  175. if (u != NULL)
  176. {
  177. if (strcmp(u, "ignore-all") == 0)
  178. return false;
  179. if (strcmp(u, "report-all") == 0)
  180. return true;
  181. if (strcmp(u, "ignore-in-object-files") == 0 && !sym->in_dyn())
  182. return false;
  183. if (strcmp(u, "ignore-in-shared-libs") == 0 && !sym->in_reg())
  184. return false;
  185. }
  186. // If the symbol is hidden, report it.
  187. if (sym->visibility() == elfcpp::STV_HIDDEN)
  188. return true;
  189. // When creating a shared library, only report unresolved symbols if
  190. // -z defs was used.
  191. if (parameters->options().shared() && !parameters->options().defs())
  192. return false;
  193. // Otherwise issue a warning.
  194. return true;
  195. }
  196. template<int size, bool big_endian>
  197. inline void
  198. issue_discarded_error(
  199. const Relocate_info<size, big_endian>* relinfo,
  200. size_t shndx,
  201. section_offset_type offset,
  202. unsigned int r_sym,
  203. const Symbol* gsym)
  204. {
  205. Sized_relobj_file<size, big_endian>* object = relinfo->object;
  206. if (gsym == NULL)
  207. {
  208. gold_error_at_location(
  209. relinfo, shndx, offset,
  210. _("relocation refers to local symbol \"%s\" [%u], "
  211. "which is defined in a discarded section"),
  212. object->get_symbol_name(r_sym), r_sym);
  213. }
  214. else
  215. {
  216. gold_error_at_location(
  217. relinfo, shndx, offset,
  218. _("relocation refers to global symbol \"%s\", "
  219. "which is defined in a discarded section"),
  220. gsym->demangled_name().c_str());
  221. }
  222. bool is_ordinary;
  223. typename elfcpp::Elf_types<size>::Elf_Addr value;
  224. unsigned int orig_shndx = object->symbol_section_and_value(r_sym, &value,
  225. &is_ordinary);
  226. if (orig_shndx != elfcpp::SHN_UNDEF)
  227. {
  228. unsigned int key_symndx = 0;
  229. Relobj* kept_obj = object->find_kept_section_object(orig_shndx,
  230. &key_symndx);
  231. if (key_symndx != 0)
  232. gold_info(_(" section group signature: \"%s\""),
  233. object->get_symbol_name(key_symndx));
  234. if (kept_obj != NULL)
  235. gold_info(_(" prevailing definition is from %s"),
  236. kept_obj->name().c_str());
  237. }
  238. }
  239. // This function implements the generic part of relocation processing.
  240. // The template parameter Relocate must be a class type which provides
  241. // a single function, relocate(), which implements the machine
  242. // specific part of a relocation.
  243. // The template parameter Relocate_comdat_behavior is a class type
  244. // which provides a single function, get(), which determines what the
  245. // linker should do for relocations that refer to discarded comdat
  246. // sections.
  247. // SIZE is the ELF size: 32 or 64. BIG_ENDIAN is the endianness of
  248. // the data. SH_TYPE is the section type: SHT_REL or SHT_RELA.
  249. // RELOCATE implements operator() to do a relocation.
  250. // PRELOCS points to the relocation data. RELOC_COUNT is the number
  251. // of relocs. OUTPUT_SECTION is the output section.
  252. // NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be
  253. // mapped to output offsets.
  254. // VIEW is the section data, VIEW_ADDRESS is its memory address, and
  255. // VIEW_SIZE is the size. These refer to the input section, unless
  256. // NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to
  257. // the output section.
  258. // RELOC_SYMBOL_CHANGES is used for -fsplit-stack support. If it is
  259. // not NULL, it is a vector indexed by relocation index. If that
  260. // entry is not NULL, it points to a global symbol which used as the
  261. // symbol for the relocation, ignoring the symbol index in the
  262. // relocation.
  263. template<int size, bool big_endian, typename Target_type,
  264. typename Relocate,
  265. typename Relocate_comdat_behavior,
  266. typename Classify_reloc>
  267. inline void
  268. relocate_section(
  269. const Relocate_info<size, big_endian>* relinfo,
  270. Target_type* target,
  271. const unsigned char* prelocs,
  272. size_t reloc_count,
  273. Output_section* output_section,
  274. bool needs_special_offset_handling,
  275. unsigned char* view,
  276. typename elfcpp::Elf_types<size>::Elf_Addr view_address,
  277. section_size_type view_size,
  278. const Reloc_symbol_changes* reloc_symbol_changes)
  279. {
  280. typedef typename Classify_reloc::Reltype Reltype;
  281. const int reloc_size = Classify_reloc::reloc_size;
  282. Relocate relocate;
  283. Relocate_comdat_behavior relocate_comdat_behavior;
  284. Sized_relobj_file<size, big_endian>* object = relinfo->object;
  285. unsigned int local_count = object->local_symbol_count();
  286. Comdat_behavior comdat_behavior = CB_UNDETERMINED;
  287. for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
  288. {
  289. Reltype reloc(prelocs);
  290. section_offset_type offset =
  291. convert_to_section_size_type(reloc.get_r_offset());
  292. if (needs_special_offset_handling)
  293. {
  294. offset = output_section->output_offset(relinfo->object,
  295. relinfo->data_shndx,
  296. offset);
  297. if (offset == -1)
  298. continue;
  299. }
  300. unsigned int r_sym = Classify_reloc::get_r_sym(&reloc);
  301. const Sized_symbol<size>* sym;
  302. Symbol_value<size> symval;
  303. const Symbol_value<size> *psymval;
  304. bool is_defined_in_discarded_section;
  305. unsigned int shndx;
  306. const Symbol* gsym = NULL;
  307. if (r_sym < local_count
  308. && (reloc_symbol_changes == NULL
  309. || (*reloc_symbol_changes)[i] == NULL))
  310. {
  311. sym = NULL;
  312. psymval = object->local_symbol(r_sym);
  313. // If the local symbol belongs to a section we are discarding,
  314. // and that section is a debug section, try to find the
  315. // corresponding kept section and map this symbol to its
  316. // counterpart in the kept section. The symbol must not
  317. // correspond to a section we are folding.
  318. bool is_ordinary;
  319. shndx = psymval->input_shndx(&is_ordinary);
  320. is_defined_in_discarded_section =
  321. (is_ordinary
  322. && shndx != elfcpp::SHN_UNDEF
  323. && !object->is_section_included(shndx)
  324. && !relinfo->symtab->is_section_folded(object, shndx));
  325. }
  326. else
  327. {
  328. if (reloc_symbol_changes != NULL
  329. && (*reloc_symbol_changes)[i] != NULL)
  330. gsym = (*reloc_symbol_changes)[i];
  331. else
  332. {
  333. gsym = object->global_symbol(r_sym);
  334. gold_assert(gsym != NULL);
  335. if (gsym->is_forwarder())
  336. gsym = relinfo->symtab->resolve_forwards(gsym);
  337. }
  338. sym = static_cast<const Sized_symbol<size>*>(gsym);
  339. if (sym->has_symtab_index() && sym->symtab_index() != -1U)
  340. symval.set_output_symtab_index(sym->symtab_index());
  341. else
  342. symval.set_no_output_symtab_entry();
  343. symval.set_output_value(sym->value());
  344. if (gsym->type() == elfcpp::STT_TLS)
  345. symval.set_is_tls_symbol();
  346. else if (gsym->type() == elfcpp::STT_GNU_IFUNC)
  347. symval.set_is_ifunc_symbol();
  348. psymval = &symval;
  349. is_defined_in_discarded_section =
  350. (gsym->is_defined_in_discarded_section()
  351. && gsym->is_undefined());
  352. shndx = 0;
  353. }
  354. Symbol_value<size> symval2;
  355. if (is_defined_in_discarded_section)
  356. {
  357. std::string name = object->section_name(relinfo->data_shndx);
  358. if (comdat_behavior == CB_UNDETERMINED)
  359. comdat_behavior = relocate_comdat_behavior.get(name.c_str());
  360. if (comdat_behavior == CB_PRETEND)
  361. {
  362. // FIXME: This case does not work for global symbols.
  363. // We have no place to store the original section index.
  364. // Fortunately this does not matter for comdat sections,
  365. // only for sections explicitly discarded by a linker
  366. // script.
  367. bool found;
  368. typename elfcpp::Elf_types<size>::Elf_Addr value =
  369. object->map_to_kept_section(shndx, name, &found);
  370. if (found)
  371. symval2.set_output_value(value + psymval->input_value());
  372. else
  373. symval2.set_output_value(0);
  374. }
  375. else
  376. {
  377. if (comdat_behavior == CB_ERROR)
  378. issue_discarded_error(relinfo, i, offset, r_sym, gsym);
  379. symval2.set_output_value(0);
  380. }
  381. symval2.set_no_output_symtab_entry();
  382. psymval = &symval2;
  383. }
  384. // If OFFSET is out of range, still let the target decide to
  385. // ignore the relocation. Pass in NULL as the VIEW argument so
  386. // that it can return quickly without trashing an invalid memory
  387. // address.
  388. unsigned char *v = view + offset;
  389. if (offset < 0 || static_cast<section_size_type>(offset) >= view_size)
  390. v = NULL;
  391. if (!relocate.relocate(relinfo, Classify_reloc::sh_type, target,
  392. output_section, i, prelocs, sym, psymval,
  393. v, view_address + offset, view_size))
  394. continue;
  395. if (v == NULL)
  396. {
  397. gold_error_at_location(relinfo, i, offset,
  398. _("reloc has bad offset %zu"),
  399. static_cast<size_t>(offset));
  400. continue;
  401. }
  402. if (issue_undefined_symbol_error(sym))
  403. gold_undefined_symbol_at_location(sym, relinfo, i, offset);
  404. else if (sym != NULL
  405. && sym->visibility() != elfcpp::STV_DEFAULT
  406. && (sym->is_strong_undefined() || sym->is_from_dynobj()))
  407. visibility_error(sym);
  408. if (sym != NULL && sym->has_warning())
  409. relinfo->symtab->issue_warning(sym, relinfo, i, offset);
  410. }
  411. }
  412. // Apply an incremental relocation.
  413. template<int size, bool big_endian, typename Target_type,
  414. typename Relocate>
  415. void
  416. apply_relocation(const Relocate_info<size, big_endian>* relinfo,
  417. Target_type* target,
  418. typename elfcpp::Elf_types<size>::Elf_Addr r_offset,
  419. unsigned int r_type,
  420. typename elfcpp::Elf_types<size>::Elf_Swxword r_addend,
  421. const Symbol* gsym,
  422. unsigned char* view,
  423. typename elfcpp::Elf_types<size>::Elf_Addr address,
  424. section_size_type view_size)
  425. {
  426. // Construct the ELF relocation in a temporary buffer.
  427. const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
  428. unsigned char relbuf[reloc_size];
  429. elfcpp::Rela_write<size, big_endian> orel(relbuf);
  430. orel.put_r_offset(r_offset);
  431. orel.put_r_info(elfcpp::elf_r_info<size>(0, r_type));
  432. orel.put_r_addend(r_addend);
  433. // Setup a Symbol_value for the global symbol.
  434. const Sized_symbol<size>* sym = static_cast<const Sized_symbol<size>*>(gsym);
  435. Symbol_value<size> symval;
  436. gold_assert(sym->has_symtab_index() && sym->symtab_index() != -1U);
  437. symval.set_output_symtab_index(sym->symtab_index());
  438. symval.set_output_value(sym->value());
  439. if (gsym->type() == elfcpp::STT_TLS)
  440. symval.set_is_tls_symbol();
  441. else if (gsym->type() == elfcpp::STT_GNU_IFUNC)
  442. symval.set_is_ifunc_symbol();
  443. Relocate relocate;
  444. relocate.relocate(relinfo, elfcpp::SHT_RELA, target, NULL,
  445. -1U, relbuf, sym, &symval,
  446. view + r_offset, address + r_offset, view_size);
  447. }
  448. // A class for inquiring about properties of a relocation,
  449. // used while scanning relocs during a relocatable link and
  450. // garbage collection. This class may be used as the default
  451. // for SHT_RELA targets, but SHT_REL targets must implement
  452. // a derived class that overrides get_size_for_reloc.
  453. // The MIPS-64 target also needs to override the methods
  454. // for accessing the r_sym and r_type fields of a relocation,
  455. // due to its non-standard use of the r_info field.
  456. template<int sh_type_, int size, bool big_endian>
  457. class Default_classify_reloc
  458. {
  459. public:
  460. typedef typename Reloc_types<sh_type_, size, big_endian>::Reloc
  461. Reltype;
  462. typedef typename Reloc_types<sh_type_, size, big_endian>::Reloc_write
  463. Reltype_write;
  464. static const int reloc_size =
  465. Reloc_types<sh_type_, size, big_endian>::reloc_size;
  466. static const int sh_type = sh_type_;
  467. // Return the symbol referred to by the relocation.
  468. static inline unsigned int
  469. get_r_sym(const Reltype* reloc)
  470. { return elfcpp::elf_r_sym<size>(reloc->get_r_info()); }
  471. // Return the type of the relocation.
  472. static inline unsigned int
  473. get_r_type(const Reltype* reloc)
  474. { return elfcpp::elf_r_type<size>(reloc->get_r_info()); }
  475. // Return the explicit addend of the relocation (return 0 for SHT_REL).
  476. static inline typename elfcpp::Elf_types<size>::Elf_Swxword
  477. get_r_addend(const Reltype* reloc)
  478. { return Reloc_types<sh_type_, size, big_endian>::get_reloc_addend(reloc); }
  479. // Write the r_info field to a new reloc, using the r_info field from
  480. // the original reloc, replacing the r_sym field with R_SYM.
  481. static inline void
  482. put_r_info(Reltype_write* new_reloc, Reltype* reloc, unsigned int r_sym)
  483. {
  484. unsigned int r_type = elfcpp::elf_r_type<size>(reloc->get_r_info());
  485. new_reloc->put_r_info(elfcpp::elf_r_info<size>(r_sym, r_type));
  486. }
  487. // Write the r_addend field to a new reloc.
  488. static inline void
  489. put_r_addend(Reltype_write* to,
  490. typename elfcpp::Elf_types<size>::Elf_Swxword addend)
  491. { Reloc_types<sh_type_, size, big_endian>::set_reloc_addend(to, addend); }
  492. // Return the size of the addend of the relocation (only used for SHT_REL).
  493. static unsigned int
  494. get_size_for_reloc(unsigned int, Relobj*)
  495. {
  496. gold_unreachable();
  497. return 0;
  498. }
  499. };
  500. // This class may be used as a typical class for the
  501. // Scan_relocatable_reloc parameter to scan_relocatable_relocs.
  502. // This class is intended to capture the most typical target behaviour,
  503. // while still permitting targets to define their own independent class
  504. // for Scan_relocatable_reloc.
  505. template<typename Classify_reloc>
  506. class Default_scan_relocatable_relocs
  507. {
  508. public:
  509. typedef typename Classify_reloc::Reltype Reltype;
  510. static const int reloc_size = Classify_reloc::reloc_size;
  511. static const int sh_type = Classify_reloc::sh_type;
  512. // Return the symbol referred to by the relocation.
  513. static inline unsigned int
  514. get_r_sym(const Reltype* reloc)
  515. { return Classify_reloc::get_r_sym(reloc); }
  516. // Return the type of the relocation.
  517. static inline unsigned int
  518. get_r_type(const Reltype* reloc)
  519. { return Classify_reloc::get_r_type(reloc); }
  520. // Return the strategy to use for a local symbol which is not a
  521. // section symbol, given the relocation type.
  522. inline Relocatable_relocs::Reloc_strategy
  523. local_non_section_strategy(unsigned int r_type, Relobj*, unsigned int r_sym)
  524. {
  525. // We assume that relocation type 0 is NONE. Targets which are
  526. // different must override.
  527. if (r_type == 0 && r_sym == 0)
  528. return Relocatable_relocs::RELOC_DISCARD;
  529. return Relocatable_relocs::RELOC_COPY;
  530. }
  531. // Return the strategy to use for a local symbol which is a section
  532. // symbol, given the relocation type.
  533. inline Relocatable_relocs::Reloc_strategy
  534. local_section_strategy(unsigned int r_type, Relobj* object)
  535. {
  536. if (sh_type == elfcpp::SHT_RELA)
  537. return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
  538. else
  539. {
  540. switch (Classify_reloc::get_size_for_reloc(r_type, object))
  541. {
  542. case 0:
  543. return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0;
  544. case 1:
  545. return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_1;
  546. case 2:
  547. return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_2;
  548. case 4:
  549. return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4;
  550. case 8:
  551. return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_8;
  552. default:
  553. gold_unreachable();
  554. }
  555. }
  556. }
  557. // Return the strategy to use for a global symbol, given the
  558. // relocation type, the object, and the symbol index.
  559. inline Relocatable_relocs::Reloc_strategy
  560. global_strategy(unsigned int, Relobj*, unsigned int)
  561. { return Relocatable_relocs::RELOC_COPY; }
  562. };
  563. // This is a strategy class used with scan_relocatable_relocs
  564. // and --emit-relocs.
  565. template<typename Classify_reloc>
  566. class Default_emit_relocs_strategy
  567. {
  568. public:
  569. typedef typename Classify_reloc::Reltype Reltype;
  570. static const int reloc_size = Classify_reloc::reloc_size;
  571. static const int sh_type = Classify_reloc::sh_type;
  572. // Return the symbol referred to by the relocation.
  573. static inline unsigned int
  574. get_r_sym(const Reltype* reloc)
  575. { return Classify_reloc::get_r_sym(reloc); }
  576. // Return the type of the relocation.
  577. static inline unsigned int
  578. get_r_type(const Reltype* reloc)
  579. { return Classify_reloc::get_r_type(reloc); }
  580. // A local non-section symbol.
  581. inline Relocatable_relocs::Reloc_strategy
  582. local_non_section_strategy(unsigned int, Relobj*, unsigned int)
  583. { return Relocatable_relocs::RELOC_COPY; }
  584. // A local section symbol.
  585. inline Relocatable_relocs::Reloc_strategy
  586. local_section_strategy(unsigned int, Relobj*)
  587. {
  588. if (sh_type == elfcpp::SHT_RELA)
  589. return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
  590. else
  591. {
  592. // The addend is stored in the section contents. Since this
  593. // is not a relocatable link, we are going to apply the
  594. // relocation contents to the section as usual. This means
  595. // that we have no way to record the original addend. If the
  596. // original addend is not zero, there is basically no way for
  597. // the user to handle this correctly. Caveat emptor.
  598. return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0;
  599. }
  600. }
  601. // A global symbol.
  602. inline Relocatable_relocs::Reloc_strategy
  603. global_strategy(unsigned int, Relobj*, unsigned int)
  604. { return Relocatable_relocs::RELOC_COPY; }
  605. };
  606. // Scan relocs during a relocatable link. This is a default
  607. // definition which should work for most targets.
  608. // Scan_relocatable_reloc must name a class type which provides three
  609. // functions which return a Relocatable_relocs::Reloc_strategy code:
  610. // global_strategy, local_non_section_strategy, and
  611. // local_section_strategy. Most targets should be able to use
  612. // Default_scan_relocatable_relocs as this class.
  613. template<int size, bool big_endian, typename Scan_relocatable_reloc>
  614. void
  615. scan_relocatable_relocs(
  616. Symbol_table*,
  617. Layout*,
  618. Sized_relobj_file<size, big_endian>* object,
  619. unsigned int data_shndx,
  620. const unsigned char* prelocs,
  621. size_t reloc_count,
  622. Output_section* output_section,
  623. bool needs_special_offset_handling,
  624. size_t local_symbol_count,
  625. const unsigned char* plocal_syms,
  626. Relocatable_relocs* rr)
  627. {
  628. typedef typename Scan_relocatable_reloc::Reltype Reltype;
  629. const int reloc_size = Scan_relocatable_reloc::reloc_size;
  630. const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
  631. Scan_relocatable_reloc scan;
  632. for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
  633. {
  634. Reltype reloc(prelocs);
  635. Relocatable_relocs::Reloc_strategy strategy;
  636. if (needs_special_offset_handling
  637. && !output_section->is_input_address_mapped(object, data_shndx,
  638. reloc.get_r_offset()))
  639. strategy = Relocatable_relocs::RELOC_DISCARD;
  640. else
  641. {
  642. const unsigned int r_sym = Scan_relocatable_reloc::get_r_sym(&reloc);
  643. const unsigned int r_type =
  644. Scan_relocatable_reloc::get_r_type(&reloc);
  645. if (r_sym >= local_symbol_count)
  646. strategy = scan.global_strategy(r_type, object, r_sym);
  647. else
  648. {
  649. gold_assert(plocal_syms != NULL);
  650. typename elfcpp::Sym<size, big_endian> lsym(plocal_syms
  651. + r_sym * sym_size);
  652. unsigned int shndx = lsym.get_st_shndx();
  653. bool is_ordinary;
  654. shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
  655. if (is_ordinary
  656. && shndx != elfcpp::SHN_UNDEF
  657. && !object->is_section_included(shndx))
  658. {
  659. // RELOC is a relocation against a local symbol
  660. // defined in a section we are discarding. Discard
  661. // the reloc. FIXME: Should we issue a warning?
  662. strategy = Relocatable_relocs::RELOC_DISCARD;
  663. }
  664. else if (lsym.get_st_type() != elfcpp::STT_SECTION)
  665. strategy = scan.local_non_section_strategy(r_type, object,
  666. r_sym);
  667. else
  668. {
  669. strategy = scan.local_section_strategy(r_type, object);
  670. if (strategy != Relocatable_relocs::RELOC_DISCARD)
  671. object->output_section(shndx)->set_needs_symtab_index();
  672. }
  673. if (strategy == Relocatable_relocs::RELOC_COPY)
  674. object->set_must_have_output_symtab_entry(r_sym);
  675. }
  676. }
  677. rr->set_next_reloc_strategy(strategy);
  678. }
  679. }
  680. // Relocate relocs. Called for a relocatable link, and for --emit-relocs.
  681. // This is a default definition which should work for most targets.
  682. template<int size, bool big_endian, typename Classify_reloc>
  683. void
  684. relocate_relocs(
  685. const Relocate_info<size, big_endian>* relinfo,
  686. const unsigned char* prelocs,
  687. size_t reloc_count,
  688. Output_section* output_section,
  689. typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
  690. unsigned char* view,
  691. typename elfcpp::Elf_types<size>::Elf_Addr view_address,
  692. section_size_type view_size,
  693. unsigned char* reloc_view,
  694. section_size_type reloc_view_size)
  695. {
  696. typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
  697. typedef typename Classify_reloc::Reltype Reltype;
  698. typedef typename Classify_reloc::Reltype_write Reltype_write;
  699. const int reloc_size = Classify_reloc::reloc_size;
  700. const Address invalid_address = static_cast<Address>(0) - 1;
  701. Sized_relobj_file<size, big_endian>* const object = relinfo->object;
  702. const unsigned int local_count = object->local_symbol_count();
  703. unsigned char* pwrite = reloc_view;
  704. const bool relocatable = parameters->options().relocatable();
  705. for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
  706. {
  707. Relocatable_relocs::Reloc_strategy strategy = relinfo->rr->strategy(i);
  708. if (strategy == Relocatable_relocs::RELOC_DISCARD)
  709. continue;
  710. if (strategy == Relocatable_relocs::RELOC_SPECIAL)
  711. {
  712. // Target wants to handle this relocation.
  713. Sized_target<size, big_endian>* target =
  714. parameters->sized_target<size, big_endian>();
  715. target->relocate_special_relocatable(relinfo, Classify_reloc::sh_type,
  716. prelocs, i, output_section,
  717. offset_in_output_section,
  718. view, view_address,
  719. view_size, pwrite);
  720. pwrite += reloc_size;
  721. continue;
  722. }
  723. Reltype reloc(prelocs);
  724. Reltype_write reloc_write(pwrite);
  725. const unsigned int r_sym = Classify_reloc::get_r_sym(&reloc);
  726. // Get the new symbol index.
  727. Output_section* os = NULL;
  728. unsigned int new_symndx;
  729. if (r_sym < local_count)
  730. {
  731. switch (strategy)
  732. {
  733. case Relocatable_relocs::RELOC_COPY:
  734. if (r_sym == 0)
  735. new_symndx = 0;
  736. else
  737. {
  738. new_symndx = object->symtab_index(r_sym);
  739. gold_assert(new_symndx != -1U);
  740. }
  741. break;
  742. case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
  743. case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0:
  744. case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_1:
  745. case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_2:
  746. case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4:
  747. case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_8:
  748. case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4_UNALIGNED:
  749. {
  750. // We are adjusting a section symbol. We need to find
  751. // the symbol table index of the section symbol for
  752. // the output section corresponding to input section
  753. // in which this symbol is defined.
  754. gold_assert(r_sym < local_count);
  755. bool is_ordinary;
  756. unsigned int shndx =
  757. object->local_symbol_input_shndx(r_sym, &is_ordinary);
  758. gold_assert(is_ordinary);
  759. os = object->output_section(shndx);
  760. gold_assert(os != NULL);
  761. gold_assert(os->needs_symtab_index());
  762. new_symndx = os->symtab_index();
  763. }
  764. break;
  765. default:
  766. gold_unreachable();
  767. }
  768. }
  769. else
  770. {
  771. const Symbol* gsym = object->global_symbol(r_sym);
  772. gold_assert(gsym != NULL);
  773. if (gsym->is_forwarder())
  774. gsym = relinfo->symtab->resolve_forwards(gsym);
  775. gold_assert(gsym->has_symtab_index());
  776. new_symndx = gsym->symtab_index();
  777. }
  778. // Get the new offset--the location in the output section where
  779. // this relocation should be applied.
  780. Address offset = reloc.get_r_offset();
  781. Address new_offset;
  782. if (offset_in_output_section != invalid_address)
  783. new_offset = offset + offset_in_output_section;
  784. else
  785. {
  786. section_offset_type sot_offset =
  787. convert_types<section_offset_type, Address>(offset);
  788. section_offset_type new_sot_offset =
  789. output_section->output_offset(object, relinfo->data_shndx,
  790. sot_offset);
  791. gold_assert(new_sot_offset != -1);
  792. new_offset = new_sot_offset;
  793. }
  794. // In an object file, r_offset is an offset within the section.
  795. // In an executable or dynamic object, generated by
  796. // --emit-relocs, r_offset is an absolute address.
  797. if (!relocatable)
  798. {
  799. new_offset += view_address;
  800. if (offset_in_output_section != invalid_address)
  801. new_offset -= offset_in_output_section;
  802. }
  803. reloc_write.put_r_offset(new_offset);
  804. Classify_reloc::put_r_info(&reloc_write, &reloc, new_symndx);
  805. // Handle the reloc addend based on the strategy.
  806. if (strategy == Relocatable_relocs::RELOC_COPY)
  807. {
  808. if (Classify_reloc::sh_type == elfcpp::SHT_RELA)
  809. Classify_reloc::put_r_addend(&reloc_write,
  810. Classify_reloc::get_r_addend(&reloc));
  811. }
  812. else
  813. {
  814. // The relocation uses a section symbol in the input file.
  815. // We are adjusting it to use a section symbol in the output
  816. // file. The input section symbol refers to some address in
  817. // the input section. We need the relocation in the output
  818. // file to refer to that same address. This adjustment to
  819. // the addend is the same calculation we use for a simple
  820. // absolute relocation for the input section symbol.
  821. const Symbol_value<size>* psymval = object->local_symbol(r_sym);
  822. unsigned char* padd = view + offset;
  823. switch (strategy)
  824. {
  825. case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
  826. {
  827. typename elfcpp::Elf_types<size>::Elf_Swxword addend
  828. = Classify_reloc::get_r_addend(&reloc);
  829. addend = psymval->value(object, addend);
  830. // In a relocatable link, the symbol value is relative to
  831. // the start of the output section. For a non-relocatable
  832. // link, we need to adjust the addend.
  833. if (!relocatable)
  834. {
  835. gold_assert(os != NULL);
  836. addend -= os->address();
  837. }
  838. Classify_reloc::put_r_addend(&reloc_write, addend);
  839. }
  840. break;
  841. case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0:
  842. break;
  843. case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_1:
  844. Relocate_functions<size, big_endian>::rel8(padd, object,
  845. psymval);
  846. break;
  847. case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_2:
  848. Relocate_functions<size, big_endian>::rel16(padd, object,
  849. psymval);
  850. break;
  851. case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4:
  852. Relocate_functions<size, big_endian>::rel32(padd, object,
  853. psymval);
  854. break;
  855. case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_8:
  856. Relocate_functions<size, big_endian>::rel64(padd, object,
  857. psymval);
  858. break;
  859. case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4_UNALIGNED:
  860. Relocate_functions<size, big_endian>::rel32_unaligned(padd,
  861. object,
  862. psymval);
  863. break;
  864. default:
  865. gold_unreachable();
  866. }
  867. }
  868. pwrite += reloc_size;
  869. }
  870. gold_assert(static_cast<section_size_type>(pwrite - reloc_view)
  871. == reloc_view_size);
  872. }
  873. } // End namespace gold.
  874. #endif // !defined(GOLD_TARGET_RELOC_H)