target-reloc.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  1. // target-reloc.h -- target specific relocation support -*- C++ -*-
  2. // Copyright (C) 2006-2015 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, int sh_type,
  33. typename Scan>
  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 Reloc_types<sh_type, size, big_endian>::Reloc Reltype;
  49. const int reloc_size = Reloc_types<sh_type, size, big_endian>::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. typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
  60. unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
  61. unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
  62. if (r_sym < local_count)
  63. {
  64. gold_assert(plocal_syms != NULL);
  65. typename elfcpp::Sym<size, big_endian> lsym(plocal_syms
  66. + r_sym * sym_size);
  67. unsigned int shndx = lsym.get_st_shndx();
  68. bool is_ordinary;
  69. shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
  70. // If RELOC is a relocation against a local symbol in a
  71. // section we are discarding then we can ignore it. It will
  72. // eventually become a reloc against the value zero.
  73. //
  74. // FIXME: We should issue a warning if this is an
  75. // allocated section; is this the best place to do it?
  76. //
  77. // FIXME: The old GNU linker would in some cases look
  78. // for the linkonce section which caused this section to
  79. // be discarded, and, if the other section was the same
  80. // size, change the reloc to refer to the other section.
  81. // That seems risky and weird to me, and I don't know of
  82. // any case where it is actually required.
  83. bool is_discarded = (is_ordinary
  84. && shndx != elfcpp::SHN_UNDEF
  85. && !object->is_section_included(shndx)
  86. && !symtab->is_section_folded(object, shndx));
  87. scan.local(symtab, layout, target, object, data_shndx,
  88. output_section, reloc, r_type, lsym, is_discarded);
  89. }
  90. else
  91. {
  92. Symbol* gsym = object->global_symbol(r_sym);
  93. gold_assert(gsym != NULL);
  94. if (gsym->is_forwarder())
  95. gsym = symtab->resolve_forwards(gsym);
  96. scan.global(symtab, layout, target, object, data_shndx,
  97. output_section, reloc, r_type, gsym);
  98. }
  99. }
  100. }
  101. // Behavior for relocations to discarded comdat sections.
  102. enum Comdat_behavior
  103. {
  104. CB_UNDETERMINED, // Not yet determined -- need to look at section name.
  105. CB_PRETEND, // Attempt to map to the corresponding kept section.
  106. CB_IGNORE, // Ignore the relocation.
  107. CB_WARNING // Print a warning.
  108. };
  109. class Default_comdat_behavior
  110. {
  111. public:
  112. // Decide what the linker should do for relocations that refer to
  113. // discarded comdat sections. This decision is based on the name of
  114. // the section being relocated.
  115. inline Comdat_behavior
  116. get(const char* name)
  117. {
  118. if (Layout::is_debug_info_section(name))
  119. return CB_PRETEND;
  120. if (strcmp(name, ".eh_frame") == 0
  121. || strcmp(name, ".gcc_except_table") == 0)
  122. return CB_IGNORE;
  123. return CB_WARNING;
  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. if (sym->is_defined_in_discarded_section())
  166. return false;
  167. // If the target defines this symbol, don't report it here.
  168. if (parameters->target().is_defined_by_abi(sym))
  169. return false;
  170. // See if we've been told to ignore whether this symbol is
  171. // undefined.
  172. const char* const u = parameters->options().unresolved_symbols();
  173. if (u != NULL)
  174. {
  175. if (strcmp(u, "ignore-all") == 0)
  176. return false;
  177. if (strcmp(u, "report-all") == 0)
  178. return true;
  179. if (strcmp(u, "ignore-in-object-files") == 0 && !sym->in_dyn())
  180. return false;
  181. if (strcmp(u, "ignore-in-shared-libs") == 0 && !sym->in_reg())
  182. return false;
  183. }
  184. // If the symbol is hidden, report it.
  185. if (sym->visibility() == elfcpp::STV_HIDDEN)
  186. return true;
  187. // When creating a shared library, only report unresolved symbols if
  188. // -z defs was used.
  189. if (parameters->options().shared() && !parameters->options().defs())
  190. return false;
  191. // Otherwise issue a warning.
  192. return true;
  193. }
  194. // This function implements the generic part of relocation processing.
  195. // The template parameter Relocate must be a class type which provides
  196. // a single function, relocate(), which implements the machine
  197. // specific part of a relocation.
  198. // The template parameter Relocate_comdat_behavior is a class type
  199. // which provides a single function, get(), which determines what the
  200. // linker should do for relocations that refer to discarded comdat
  201. // sections.
  202. // SIZE is the ELF size: 32 or 64. BIG_ENDIAN is the endianness of
  203. // the data. SH_TYPE is the section type: SHT_REL or SHT_RELA.
  204. // RELOCATE implements operator() to do a relocation.
  205. // PRELOCS points to the relocation data. RELOC_COUNT is the number
  206. // of relocs. OUTPUT_SECTION is the output section.
  207. // NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be
  208. // mapped to output offsets.
  209. // VIEW is the section data, VIEW_ADDRESS is its memory address, and
  210. // VIEW_SIZE is the size. These refer to the input section, unless
  211. // NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to
  212. // the output section.
  213. // RELOC_SYMBOL_CHANGES is used for -fsplit-stack support. If it is
  214. // not NULL, it is a vector indexed by relocation index. If that
  215. // entry is not NULL, it points to a global symbol which used as the
  216. // symbol for the relocation, ignoring the symbol index in the
  217. // relocation.
  218. template<int size, bool big_endian, typename Target_type, int sh_type,
  219. typename Relocate,
  220. typename Relocate_comdat_behavior>
  221. inline void
  222. relocate_section(
  223. const Relocate_info<size, big_endian>* relinfo,
  224. Target_type* target,
  225. const unsigned char* prelocs,
  226. size_t reloc_count,
  227. Output_section* output_section,
  228. bool needs_special_offset_handling,
  229. unsigned char* view,
  230. typename elfcpp::Elf_types<size>::Elf_Addr view_address,
  231. section_size_type view_size,
  232. const Reloc_symbol_changes* reloc_symbol_changes)
  233. {
  234. typedef typename Reloc_types<sh_type, size, big_endian>::Reloc Reltype;
  235. const int reloc_size = Reloc_types<sh_type, size, big_endian>::reloc_size;
  236. Relocate relocate;
  237. Relocate_comdat_behavior relocate_comdat_behavior;
  238. Sized_relobj_file<size, big_endian>* object = relinfo->object;
  239. unsigned int local_count = object->local_symbol_count();
  240. Comdat_behavior comdat_behavior = CB_UNDETERMINED;
  241. for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
  242. {
  243. Reltype reloc(prelocs);
  244. section_offset_type offset =
  245. convert_to_section_size_type(reloc.get_r_offset());
  246. if (needs_special_offset_handling)
  247. {
  248. offset = output_section->output_offset(relinfo->object,
  249. relinfo->data_shndx,
  250. offset);
  251. if (offset == -1)
  252. continue;
  253. }
  254. typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
  255. unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
  256. unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
  257. const Sized_symbol<size>* sym;
  258. Symbol_value<size> symval;
  259. const Symbol_value<size> *psymval;
  260. bool is_defined_in_discarded_section;
  261. unsigned int shndx;
  262. if (r_sym < local_count
  263. && (reloc_symbol_changes == NULL
  264. || (*reloc_symbol_changes)[i] == NULL))
  265. {
  266. sym = NULL;
  267. psymval = object->local_symbol(r_sym);
  268. // If the local symbol belongs to a section we are discarding,
  269. // and that section is a debug section, try to find the
  270. // corresponding kept section and map this symbol to its
  271. // counterpart in the kept section. The symbol must not
  272. // correspond to a section we are folding.
  273. bool is_ordinary;
  274. shndx = psymval->input_shndx(&is_ordinary);
  275. is_defined_in_discarded_section =
  276. (is_ordinary
  277. && shndx != elfcpp::SHN_UNDEF
  278. && !object->is_section_included(shndx)
  279. && !relinfo->symtab->is_section_folded(object, shndx));
  280. }
  281. else
  282. {
  283. const Symbol* gsym;
  284. if (reloc_symbol_changes != NULL
  285. && (*reloc_symbol_changes)[i] != NULL)
  286. gsym = (*reloc_symbol_changes)[i];
  287. else
  288. {
  289. gsym = object->global_symbol(r_sym);
  290. gold_assert(gsym != NULL);
  291. if (gsym->is_forwarder())
  292. gsym = relinfo->symtab->resolve_forwards(gsym);
  293. }
  294. sym = static_cast<const Sized_symbol<size>*>(gsym);
  295. if (sym->has_symtab_index() && sym->symtab_index() != -1U)
  296. symval.set_output_symtab_index(sym->symtab_index());
  297. else
  298. symval.set_no_output_symtab_entry();
  299. symval.set_output_value(sym->value());
  300. if (gsym->type() == elfcpp::STT_TLS)
  301. symval.set_is_tls_symbol();
  302. else if (gsym->type() == elfcpp::STT_GNU_IFUNC)
  303. symval.set_is_ifunc_symbol();
  304. psymval = &symval;
  305. is_defined_in_discarded_section =
  306. (gsym->is_defined_in_discarded_section()
  307. && gsym->is_undefined());
  308. shndx = 0;
  309. }
  310. Symbol_value<size> symval2;
  311. if (is_defined_in_discarded_section)
  312. {
  313. if (comdat_behavior == CB_UNDETERMINED)
  314. {
  315. std::string name = object->section_name(relinfo->data_shndx);
  316. comdat_behavior = relocate_comdat_behavior.get(name.c_str());
  317. }
  318. if (comdat_behavior == CB_PRETEND)
  319. {
  320. // FIXME: This case does not work for global symbols.
  321. // We have no place to store the original section index.
  322. // Fortunately this does not matter for comdat sections,
  323. // only for sections explicitly discarded by a linker
  324. // script.
  325. bool found;
  326. typename elfcpp::Elf_types<size>::Elf_Addr value =
  327. object->map_to_kept_section(shndx, &found);
  328. if (found)
  329. symval2.set_output_value(value + psymval->input_value());
  330. else
  331. symval2.set_output_value(0);
  332. }
  333. else
  334. {
  335. if (comdat_behavior == CB_WARNING)
  336. gold_warning_at_location(relinfo, i, offset,
  337. _("relocation refers to discarded "
  338. "section"));
  339. symval2.set_output_value(0);
  340. }
  341. symval2.set_no_output_symtab_entry();
  342. psymval = &symval2;
  343. }
  344. // If OFFSET is out of range, still let the target decide to
  345. // ignore the relocation. Pass in NULL as the VIEW argument so
  346. // that it can return quickly without trashing an invalid memory
  347. // address.
  348. unsigned char *v = view + offset;
  349. if (offset < 0 || static_cast<section_size_type>(offset) >= view_size)
  350. v = NULL;
  351. if (!relocate.relocate(relinfo, target, output_section, i, reloc,
  352. r_type, sym, psymval, v, view_address + offset,
  353. view_size))
  354. continue;
  355. if (v == NULL)
  356. {
  357. gold_error_at_location(relinfo, i, offset,
  358. _("reloc has bad offset %zu"),
  359. static_cast<size_t>(offset));
  360. continue;
  361. }
  362. if (issue_undefined_symbol_error(sym))
  363. gold_undefined_symbol_at_location(sym, relinfo, i, offset);
  364. else if (sym != NULL
  365. && sym->visibility() != elfcpp::STV_DEFAULT
  366. && (sym->is_strong_undefined() || sym->is_from_dynobj()))
  367. visibility_error(sym);
  368. if (sym != NULL && sym->has_warning())
  369. relinfo->symtab->issue_warning(sym, relinfo, i, offset);
  370. }
  371. }
  372. // Apply an incremental relocation.
  373. template<int size, bool big_endian, typename Target_type,
  374. typename Relocate>
  375. void
  376. apply_relocation(const Relocate_info<size, big_endian>* relinfo,
  377. Target_type* target,
  378. typename elfcpp::Elf_types<size>::Elf_Addr r_offset,
  379. unsigned int r_type,
  380. typename elfcpp::Elf_types<size>::Elf_Swxword r_addend,
  381. const Symbol* gsym,
  382. unsigned char* view,
  383. typename elfcpp::Elf_types<size>::Elf_Addr address,
  384. section_size_type view_size)
  385. {
  386. // Construct the ELF relocation in a temporary buffer.
  387. const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
  388. unsigned char relbuf[reloc_size];
  389. elfcpp::Rela<size, big_endian> rel(relbuf);
  390. elfcpp::Rela_write<size, big_endian> orel(relbuf);
  391. orel.put_r_offset(r_offset);
  392. orel.put_r_info(elfcpp::elf_r_info<size>(0, r_type));
  393. orel.put_r_addend(r_addend);
  394. // Setup a Symbol_value for the global symbol.
  395. const Sized_symbol<size>* sym = static_cast<const Sized_symbol<size>*>(gsym);
  396. Symbol_value<size> symval;
  397. gold_assert(sym->has_symtab_index() && sym->symtab_index() != -1U);
  398. symval.set_output_symtab_index(sym->symtab_index());
  399. symval.set_output_value(sym->value());
  400. if (gsym->type() == elfcpp::STT_TLS)
  401. symval.set_is_tls_symbol();
  402. else if (gsym->type() == elfcpp::STT_GNU_IFUNC)
  403. symval.set_is_ifunc_symbol();
  404. Relocate relocate;
  405. relocate.relocate(relinfo, target, NULL, -1U, rel, r_type, sym, &symval,
  406. view + r_offset, address + r_offset, view_size);
  407. }
  408. // This class may be used as a typical class for the
  409. // Scan_relocatable_reloc parameter to scan_relocatable_relocs. The
  410. // template parameter Classify_reloc must be a class type which
  411. // provides a function get_size_for_reloc which returns the number of
  412. // bytes to which a reloc applies. This class is intended to capture
  413. // the most typical target behaviour, while still permitting targets
  414. // to define their own independent class for Scan_relocatable_reloc.
  415. template<int sh_type, typename Classify_reloc>
  416. class Default_scan_relocatable_relocs
  417. {
  418. public:
  419. // Return the strategy to use for a local symbol which is not a
  420. // section symbol, given the relocation type.
  421. inline Relocatable_relocs::Reloc_strategy
  422. local_non_section_strategy(unsigned int r_type, Relobj*, unsigned int r_sym)
  423. {
  424. // We assume that relocation type 0 is NONE. Targets which are
  425. // different must override.
  426. if (r_type == 0 && r_sym == 0)
  427. return Relocatable_relocs::RELOC_DISCARD;
  428. return Relocatable_relocs::RELOC_COPY;
  429. }
  430. // Return the strategy to use for a local symbol which is a section
  431. // symbol, given the relocation type.
  432. inline Relocatable_relocs::Reloc_strategy
  433. local_section_strategy(unsigned int r_type, Relobj* object)
  434. {
  435. if (sh_type == elfcpp::SHT_RELA)
  436. return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
  437. else
  438. {
  439. Classify_reloc classify;
  440. switch (classify.get_size_for_reloc(r_type, object))
  441. {
  442. case 0:
  443. return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0;
  444. case 1:
  445. return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_1;
  446. case 2:
  447. return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_2;
  448. case 4:
  449. return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4;
  450. case 8:
  451. return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_8;
  452. default:
  453. gold_unreachable();
  454. }
  455. }
  456. }
  457. // Return the strategy to use for a global symbol, given the
  458. // relocation type, the object, and the symbol index.
  459. inline Relocatable_relocs::Reloc_strategy
  460. global_strategy(unsigned int, Relobj*, unsigned int)
  461. { return Relocatable_relocs::RELOC_COPY; }
  462. };
  463. // Scan relocs during a relocatable link. This is a default
  464. // definition which should work for most targets.
  465. // Scan_relocatable_reloc must name a class type which provides three
  466. // functions which return a Relocatable_relocs::Reloc_strategy code:
  467. // global_strategy, local_non_section_strategy, and
  468. // local_section_strategy. Most targets should be able to use
  469. // Default_scan_relocatable_relocs as this class.
  470. template<int size, bool big_endian, int sh_type,
  471. typename Scan_relocatable_reloc>
  472. void
  473. scan_relocatable_relocs(
  474. Symbol_table*,
  475. Layout*,
  476. Sized_relobj_file<size, big_endian>* object,
  477. unsigned int data_shndx,
  478. const unsigned char* prelocs,
  479. size_t reloc_count,
  480. Output_section* output_section,
  481. bool needs_special_offset_handling,
  482. size_t local_symbol_count,
  483. const unsigned char* plocal_syms,
  484. Relocatable_relocs* rr)
  485. {
  486. typedef typename Reloc_types<sh_type, size, big_endian>::Reloc Reltype;
  487. const int reloc_size = Reloc_types<sh_type, size, big_endian>::reloc_size;
  488. const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
  489. Scan_relocatable_reloc scan;
  490. for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
  491. {
  492. Reltype reloc(prelocs);
  493. Relocatable_relocs::Reloc_strategy strategy;
  494. if (needs_special_offset_handling
  495. && !output_section->is_input_address_mapped(object, data_shndx,
  496. reloc.get_r_offset()))
  497. strategy = Relocatable_relocs::RELOC_DISCARD;
  498. else
  499. {
  500. typename elfcpp::Elf_types<size>::Elf_WXword r_info =
  501. reloc.get_r_info();
  502. const unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
  503. const unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
  504. if (r_sym >= local_symbol_count)
  505. strategy = scan.global_strategy(r_type, object, r_sym);
  506. else
  507. {
  508. gold_assert(plocal_syms != NULL);
  509. typename elfcpp::Sym<size, big_endian> lsym(plocal_syms
  510. + r_sym * sym_size);
  511. unsigned int shndx = lsym.get_st_shndx();
  512. bool is_ordinary;
  513. shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
  514. if (is_ordinary
  515. && shndx != elfcpp::SHN_UNDEF
  516. && !object->is_section_included(shndx))
  517. {
  518. // RELOC is a relocation against a local symbol
  519. // defined in a section we are discarding. Discard
  520. // the reloc. FIXME: Should we issue a warning?
  521. strategy = Relocatable_relocs::RELOC_DISCARD;
  522. }
  523. else if (lsym.get_st_type() != elfcpp::STT_SECTION)
  524. strategy = scan.local_non_section_strategy(r_type, object,
  525. r_sym);
  526. else
  527. {
  528. strategy = scan.local_section_strategy(r_type, object);
  529. if (strategy != Relocatable_relocs::RELOC_DISCARD)
  530. object->output_section(shndx)->set_needs_symtab_index();
  531. }
  532. if (strategy == Relocatable_relocs::RELOC_COPY)
  533. object->set_must_have_output_symtab_entry(r_sym);
  534. }
  535. }
  536. rr->set_next_reloc_strategy(strategy);
  537. }
  538. }
  539. // Relocate relocs. Called for a relocatable link, and for --emit-relocs.
  540. // This is a default definition which should work for most targets.
  541. template<int size, bool big_endian, int sh_type>
  542. void
  543. relocate_relocs(
  544. const Relocate_info<size, big_endian>* relinfo,
  545. const unsigned char* prelocs,
  546. size_t reloc_count,
  547. Output_section* output_section,
  548. typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
  549. const Relocatable_relocs* rr,
  550. unsigned char* view,
  551. typename elfcpp::Elf_types<size>::Elf_Addr view_address,
  552. section_size_type view_size,
  553. unsigned char* reloc_view,
  554. section_size_type reloc_view_size)
  555. {
  556. typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
  557. typedef typename Reloc_types<sh_type, size, big_endian>::Reloc Reltype;
  558. typedef typename Reloc_types<sh_type, size, big_endian>::Reloc_write
  559. Reltype_write;
  560. const int reloc_size = Reloc_types<sh_type, size, big_endian>::reloc_size;
  561. const Address invalid_address = static_cast<Address>(0) - 1;
  562. Sized_relobj_file<size, big_endian>* const object = relinfo->object;
  563. const unsigned int local_count = object->local_symbol_count();
  564. unsigned char* pwrite = reloc_view;
  565. for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
  566. {
  567. Relocatable_relocs::Reloc_strategy strategy = rr->strategy(i);
  568. if (strategy == Relocatable_relocs::RELOC_DISCARD)
  569. continue;
  570. if (strategy == Relocatable_relocs::RELOC_SPECIAL)
  571. {
  572. // Target wants to handle this relocation.
  573. Sized_target<size, big_endian>* target =
  574. parameters->sized_target<size, big_endian>();
  575. target->relocate_special_relocatable(relinfo, sh_type, prelocs,
  576. i, output_section,
  577. offset_in_output_section,
  578. view, view_address,
  579. view_size, pwrite);
  580. pwrite += reloc_size;
  581. continue;
  582. }
  583. Reltype reloc(prelocs);
  584. Reltype_write reloc_write(pwrite);
  585. typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
  586. const unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
  587. const unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
  588. // Get the new symbol index.
  589. Output_section* os = NULL;
  590. unsigned int new_symndx;
  591. if (r_sym < local_count)
  592. {
  593. switch (strategy)
  594. {
  595. case Relocatable_relocs::RELOC_COPY:
  596. if (r_sym == 0)
  597. new_symndx = 0;
  598. else
  599. {
  600. new_symndx = object->symtab_index(r_sym);
  601. gold_assert(new_symndx != -1U);
  602. }
  603. break;
  604. case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
  605. case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0:
  606. case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_1:
  607. case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_2:
  608. case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4:
  609. case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_8:
  610. case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4_UNALIGNED:
  611. {
  612. // We are adjusting a section symbol. We need to find
  613. // the symbol table index of the section symbol for
  614. // the output section corresponding to input section
  615. // in which this symbol is defined.
  616. gold_assert(r_sym < local_count);
  617. bool is_ordinary;
  618. unsigned int shndx =
  619. object->local_symbol_input_shndx(r_sym, &is_ordinary);
  620. gold_assert(is_ordinary);
  621. os = object->output_section(shndx);
  622. gold_assert(os != NULL);
  623. gold_assert(os->needs_symtab_index());
  624. new_symndx = os->symtab_index();
  625. }
  626. break;
  627. default:
  628. gold_unreachable();
  629. }
  630. }
  631. else
  632. {
  633. const Symbol* gsym = object->global_symbol(r_sym);
  634. gold_assert(gsym != NULL);
  635. if (gsym->is_forwarder())
  636. gsym = relinfo->symtab->resolve_forwards(gsym);
  637. gold_assert(gsym->has_symtab_index());
  638. new_symndx = gsym->symtab_index();
  639. }
  640. // Get the new offset--the location in the output section where
  641. // this relocation should be applied.
  642. Address offset = reloc.get_r_offset();
  643. Address new_offset;
  644. if (offset_in_output_section != invalid_address)
  645. new_offset = offset + offset_in_output_section;
  646. else
  647. {
  648. section_offset_type sot_offset =
  649. convert_types<section_offset_type, Address>(offset);
  650. section_offset_type new_sot_offset =
  651. output_section->output_offset(object, relinfo->data_shndx,
  652. sot_offset);
  653. gold_assert(new_sot_offset != -1);
  654. new_offset = new_sot_offset;
  655. }
  656. // In an object file, r_offset is an offset within the section.
  657. // In an executable or dynamic object, generated by
  658. // --emit-relocs, r_offset is an absolute address.
  659. if (!parameters->options().relocatable())
  660. {
  661. new_offset += view_address;
  662. if (offset_in_output_section != invalid_address)
  663. new_offset -= offset_in_output_section;
  664. }
  665. reloc_write.put_r_offset(new_offset);
  666. reloc_write.put_r_info(elfcpp::elf_r_info<size>(new_symndx, r_type));
  667. // Handle the reloc addend based on the strategy.
  668. if (strategy == Relocatable_relocs::RELOC_COPY)
  669. {
  670. if (sh_type == elfcpp::SHT_RELA)
  671. Reloc_types<sh_type, size, big_endian>::
  672. copy_reloc_addend(&reloc_write,
  673. &reloc);
  674. }
  675. else
  676. {
  677. // The relocation uses a section symbol in the input file.
  678. // We are adjusting it to use a section symbol in the output
  679. // file. The input section symbol refers to some address in
  680. // the input section. We need the relocation in the output
  681. // file to refer to that same address. This adjustment to
  682. // the addend is the same calculation we use for a simple
  683. // absolute relocation for the input section symbol.
  684. const Symbol_value<size>* psymval = object->local_symbol(r_sym);
  685. unsigned char* padd = view + offset;
  686. switch (strategy)
  687. {
  688. case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
  689. {
  690. typename elfcpp::Elf_types<size>::Elf_Swxword addend;
  691. addend = Reloc_types<sh_type, size, big_endian>::
  692. get_reloc_addend(&reloc);
  693. gold_assert(os != NULL);
  694. addend = psymval->value(object, addend) - os->address();
  695. Reloc_types<sh_type, size, big_endian>::
  696. set_reloc_addend(&reloc_write, addend);
  697. }
  698. break;
  699. case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0:
  700. break;
  701. case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_1:
  702. Relocate_functions<size, big_endian>::rel8(padd, object,
  703. psymval);
  704. break;
  705. case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_2:
  706. Relocate_functions<size, big_endian>::rel16(padd, object,
  707. psymval);
  708. break;
  709. case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4:
  710. Relocate_functions<size, big_endian>::rel32(padd, object,
  711. psymval);
  712. break;
  713. case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_8:
  714. Relocate_functions<size, big_endian>::rel64(padd, object,
  715. psymval);
  716. break;
  717. case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4_UNALIGNED:
  718. Relocate_functions<size, big_endian>::rel32_unaligned(padd,
  719. object,
  720. psymval);
  721. break;
  722. default:
  723. gold_unreachable();
  724. }
  725. }
  726. pwrite += reloc_size;
  727. }
  728. gold_assert(static_cast<section_size_type>(pwrite - reloc_view)
  729. == reloc_view_size);
  730. }
  731. } // End namespace gold.
  732. #endif // !defined(GOLD_TARGET_RELOC_H)