resolve.cc 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122
  1. // resolve.cc -- symbol resolution for gold
  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. #include "gold.h"
  18. #include "elfcpp.h"
  19. #include "target.h"
  20. #include "object.h"
  21. #include "symtab.h"
  22. #include "plugin.h"
  23. namespace gold
  24. {
  25. // Symbol methods used in this file.
  26. // This symbol is being overridden by another symbol whose version is
  27. // VERSION. Update the VERSION_ field accordingly.
  28. inline void
  29. Symbol::override_version(const char* version)
  30. {
  31. if (version == NULL)
  32. {
  33. // This is the case where this symbol is NAME/VERSION, and the
  34. // version was not marked as hidden. That makes it the default
  35. // version, so we create NAME/NULL. Later we see another symbol
  36. // NAME/NULL, and that symbol is overriding this one. In this
  37. // case, since NAME/VERSION is the default, we make NAME/NULL
  38. // override NAME/VERSION as well. They are already the same
  39. // Symbol structure. Setting the VERSION_ field to NULL ensures
  40. // that it will be output with the correct, empty, version.
  41. this->version_ = version;
  42. }
  43. else
  44. {
  45. // This is the case where this symbol is NAME/VERSION_ONE, and
  46. // now we see NAME/VERSION_TWO, and NAME/VERSION_TWO is
  47. // overriding NAME. If VERSION_ONE and VERSION_TWO are
  48. // different, then this can only happen when VERSION_ONE is NULL
  49. // and VERSION_TWO is not hidden.
  50. gold_assert(this->version_ == version || this->version_ == NULL);
  51. this->version_ = version;
  52. }
  53. }
  54. // This symbol is being overidden by another symbol whose visibility
  55. // is VISIBILITY. Updated the VISIBILITY_ field accordingly.
  56. inline void
  57. Symbol::override_visibility(elfcpp::STV visibility)
  58. {
  59. // The rule for combining visibility is that we always choose the
  60. // most constrained visibility. In order of increasing constraint,
  61. // visibility goes PROTECTED, HIDDEN, INTERNAL. This is the reverse
  62. // of the numeric values, so the effect is that we always want the
  63. // smallest non-zero value.
  64. if (visibility != elfcpp::STV_DEFAULT)
  65. {
  66. if (this->visibility_ == elfcpp::STV_DEFAULT)
  67. this->visibility_ = visibility;
  68. else if (this->visibility_ > visibility)
  69. this->visibility_ = visibility;
  70. }
  71. }
  72. // Override the fields in Symbol.
  73. template<int size, bool big_endian>
  74. void
  75. Symbol::override_base(const elfcpp::Sym<size, big_endian>& sym,
  76. unsigned int st_shndx, bool is_ordinary,
  77. Object* object, const char* version)
  78. {
  79. gold_assert(this->source_ == FROM_OBJECT);
  80. this->u_.from_object.object = object;
  81. this->override_version(version);
  82. this->u_.from_object.shndx = st_shndx;
  83. this->is_ordinary_shndx_ = is_ordinary;
  84. // Don't override st_type from plugin placeholder symbols.
  85. if (object->pluginobj() == NULL)
  86. this->type_ = sym.get_st_type();
  87. this->binding_ = sym.get_st_bind();
  88. this->override_visibility(sym.get_st_visibility());
  89. this->nonvis_ = sym.get_st_nonvis();
  90. if (object->is_dynamic())
  91. this->in_dyn_ = true;
  92. else
  93. this->in_reg_ = true;
  94. }
  95. // Override the fields in Sized_symbol.
  96. template<int size>
  97. template<bool big_endian>
  98. void
  99. Sized_symbol<size>::override(const elfcpp::Sym<size, big_endian>& sym,
  100. unsigned st_shndx, bool is_ordinary,
  101. Object* object, const char* version)
  102. {
  103. this->override_base(sym, st_shndx, is_ordinary, object, version);
  104. this->value_ = sym.get_st_value();
  105. this->symsize_ = sym.get_st_size();
  106. }
  107. // Override TOSYM with symbol FROMSYM, defined in OBJECT, with version
  108. // VERSION. This handles all aliases of TOSYM.
  109. template<int size, bool big_endian>
  110. void
  111. Symbol_table::override(Sized_symbol<size>* tosym,
  112. const elfcpp::Sym<size, big_endian>& fromsym,
  113. unsigned int st_shndx, bool is_ordinary,
  114. Object* object, const char* version)
  115. {
  116. tosym->override(fromsym, st_shndx, is_ordinary, object, version);
  117. if (tosym->has_alias())
  118. {
  119. Symbol* sym = this->weak_aliases_[tosym];
  120. gold_assert(sym != NULL);
  121. Sized_symbol<size>* ssym = this->get_sized_symbol<size>(sym);
  122. do
  123. {
  124. ssym->override(fromsym, st_shndx, is_ordinary, object, version);
  125. sym = this->weak_aliases_[ssym];
  126. gold_assert(sym != NULL);
  127. ssym = this->get_sized_symbol<size>(sym);
  128. }
  129. while (ssym != tosym);
  130. }
  131. }
  132. // The resolve functions build a little code for each symbol.
  133. // Bit 0: 0 for global, 1 for weak.
  134. // Bit 1: 0 for regular object, 1 for shared object
  135. // Bits 2-3: 0 for normal, 1 for undefined, 2 for common
  136. // This gives us values from 0 to 11.
  137. static const int global_or_weak_shift = 0;
  138. static const unsigned int global_flag = 0 << global_or_weak_shift;
  139. static const unsigned int weak_flag = 1 << global_or_weak_shift;
  140. static const int regular_or_dynamic_shift = 1;
  141. static const unsigned int regular_flag = 0 << regular_or_dynamic_shift;
  142. static const unsigned int dynamic_flag = 1 << regular_or_dynamic_shift;
  143. static const int def_undef_or_common_shift = 2;
  144. static const unsigned int def_flag = 0 << def_undef_or_common_shift;
  145. static const unsigned int undef_flag = 1 << def_undef_or_common_shift;
  146. static const unsigned int common_flag = 2 << def_undef_or_common_shift;
  147. // This convenience function combines all the flags based on facts
  148. // about the symbol.
  149. static unsigned int
  150. symbol_to_bits(elfcpp::STB binding, bool is_dynamic,
  151. unsigned int shndx, bool is_ordinary)
  152. {
  153. unsigned int bits;
  154. switch (binding)
  155. {
  156. case elfcpp::STB_GLOBAL:
  157. case elfcpp::STB_GNU_UNIQUE:
  158. bits = global_flag;
  159. break;
  160. case elfcpp::STB_WEAK:
  161. bits = weak_flag;
  162. break;
  163. case elfcpp::STB_LOCAL:
  164. // We should only see externally visible symbols in the symbol
  165. // table.
  166. gold_error(_("invalid STB_LOCAL symbol in external symbols"));
  167. bits = global_flag;
  168. default:
  169. // Any target which wants to handle STB_LOOS, etc., needs to
  170. // define a resolve method.
  171. gold_error(_("unsupported symbol binding %d"), static_cast<int>(binding));
  172. bits = global_flag;
  173. }
  174. if (is_dynamic)
  175. bits |= dynamic_flag;
  176. else
  177. bits |= regular_flag;
  178. switch (shndx)
  179. {
  180. case elfcpp::SHN_UNDEF:
  181. bits |= undef_flag;
  182. break;
  183. case elfcpp::SHN_COMMON:
  184. if (!is_ordinary)
  185. bits |= common_flag;
  186. break;
  187. default:
  188. if (!is_ordinary && Symbol::is_common_shndx(shndx))
  189. bits |= common_flag;
  190. else
  191. bits |= def_flag;
  192. break;
  193. }
  194. return bits;
  195. }
  196. // Resolve a symbol. This is called the second and subsequent times
  197. // we see a symbol. TO is the pre-existing symbol. ST_SHNDX is the
  198. // section index for SYM, possibly adjusted for many sections.
  199. // IS_ORDINARY is whether ST_SHNDX is a normal section index rather
  200. // than a special code. ORIG_ST_SHNDX is the original section index,
  201. // before any munging because of discarded sections, except that all
  202. // non-ordinary section indexes are mapped to SHN_UNDEF. VERSION is
  203. // the version of SYM.
  204. template<int size, bool big_endian>
  205. void
  206. Symbol_table::resolve(Sized_symbol<size>* to,
  207. const elfcpp::Sym<size, big_endian>& sym,
  208. unsigned int st_shndx, bool is_ordinary,
  209. unsigned int orig_st_shndx,
  210. Object* object, const char* version,
  211. bool is_default_version)
  212. {
  213. // It's possible for a symbol to be defined in an object file
  214. // using .symver to give it a version, and for there to also be
  215. // a linker script giving that symbol the same version. We
  216. // don't want to give a multiple-definition error for this
  217. // harmless redefinition.
  218. bool to_is_ordinary;
  219. if (to->source() == Symbol::FROM_OBJECT
  220. && to->object() == object
  221. && is_ordinary
  222. && to->is_defined()
  223. && to->shndx(&to_is_ordinary) == st_shndx
  224. && to_is_ordinary
  225. && to->value() == sym.get_st_value())
  226. return;
  227. if (parameters->target().has_resolve())
  228. {
  229. Sized_target<size, big_endian>* sized_target;
  230. sized_target = parameters->sized_target<size, big_endian>();
  231. sized_target->resolve(to, sym, object, version);
  232. return;
  233. }
  234. if (!object->is_dynamic())
  235. {
  236. if (sym.get_st_type() == elfcpp::STT_COMMON
  237. && (is_ordinary || !Symbol::is_common_shndx(st_shndx)))
  238. {
  239. gold_warning(_("STT_COMMON symbol '%s' in %s "
  240. "is not in a common section"),
  241. to->demangled_name().c_str(),
  242. to->object()->name().c_str());
  243. return;
  244. }
  245. // Record that we've seen this symbol in a regular object.
  246. to->set_in_reg();
  247. }
  248. else if (st_shndx == elfcpp::SHN_UNDEF
  249. && (to->visibility() == elfcpp::STV_HIDDEN
  250. || to->visibility() == elfcpp::STV_INTERNAL))
  251. {
  252. // The symbol is hidden, so a reference from a shared object
  253. // cannot bind to it. We tried issuing a warning in this case,
  254. // but that produces false positives when the symbol is
  255. // actually resolved in a different shared object (PR 15574).
  256. return;
  257. }
  258. else
  259. {
  260. // Record that we've seen this symbol in a dynamic object.
  261. to->set_in_dyn();
  262. }
  263. // Record if we've seen this symbol in a real ELF object (i.e., the
  264. // symbol is referenced from outside the world known to the plugin).
  265. if (object->pluginobj() == NULL && !object->is_dynamic())
  266. to->set_in_real_elf();
  267. // If we're processing replacement files, allow new symbols to override
  268. // the placeholders from the plugin objects.
  269. // Treat common symbols specially since it is possible that an ELF
  270. // file increased the size of the alignment.
  271. if (to->source() == Symbol::FROM_OBJECT)
  272. {
  273. Pluginobj* obj = to->object()->pluginobj();
  274. if (obj != NULL
  275. && parameters->options().plugins()->in_replacement_phase())
  276. {
  277. bool adjust_common = false;
  278. typename Sized_symbol<size>::Size_type tosize = 0;
  279. typename Sized_symbol<size>::Value_type tovalue = 0;
  280. if (to->is_common()
  281. && !is_ordinary && Symbol::is_common_shndx(st_shndx))
  282. {
  283. adjust_common = true;
  284. tosize = to->symsize();
  285. tovalue = to->value();
  286. }
  287. this->override(to, sym, st_shndx, is_ordinary, object, version);
  288. if (adjust_common)
  289. {
  290. if (tosize > to->symsize())
  291. to->set_symsize(tosize);
  292. if (tovalue > to->value())
  293. to->set_value(tovalue);
  294. }
  295. return;
  296. }
  297. }
  298. // A new weak undefined reference, merging with an old weak
  299. // reference, could be a One Definition Rule (ODR) violation --
  300. // especially if the types or sizes of the references differ. We'll
  301. // store such pairs and look them up later to make sure they
  302. // actually refer to the same lines of code. We also check
  303. // combinations of weak and strong, which might occur if one case is
  304. // inline and the other is not. (Note: not all ODR violations can
  305. // be found this way, and not everything this finds is an ODR
  306. // violation. But it's helpful to warn about.)
  307. if (parameters->options().detect_odr_violations()
  308. && (sym.get_st_bind() == elfcpp::STB_WEAK
  309. || to->binding() == elfcpp::STB_WEAK)
  310. && orig_st_shndx != elfcpp::SHN_UNDEF
  311. && to->shndx(&to_is_ordinary) != elfcpp::SHN_UNDEF
  312. && to_is_ordinary
  313. && sym.get_st_size() != 0 // Ignore weird 0-sized symbols.
  314. && to->symsize() != 0
  315. && (sym.get_st_type() != to->type()
  316. || sym.get_st_size() != to->symsize())
  317. // C does not have a concept of ODR, so we only need to do this
  318. // on C++ symbols. These have (mangled) names starting with _Z.
  319. && to->name()[0] == '_' && to->name()[1] == 'Z')
  320. {
  321. Symbol_location fromloc
  322. = { object, orig_st_shndx, static_cast<off_t>(sym.get_st_value()) };
  323. Symbol_location toloc = { to->object(), to->shndx(&to_is_ordinary),
  324. static_cast<off_t>(to->value()) };
  325. this->candidate_odr_violations_[to->name()].insert(fromloc);
  326. this->candidate_odr_violations_[to->name()].insert(toloc);
  327. }
  328. // Plugins don't provide a symbol type, so adopt the existing type
  329. // if the FROM symbol is from a plugin.
  330. elfcpp::STT fromtype = (object->pluginobj() != NULL
  331. ? to->type()
  332. : sym.get_st_type());
  333. unsigned int frombits = symbol_to_bits(sym.get_st_bind(),
  334. object->is_dynamic(),
  335. st_shndx, is_ordinary);
  336. bool adjust_common_sizes;
  337. bool adjust_dyndef;
  338. typename Sized_symbol<size>::Size_type tosize = to->symsize();
  339. if (Symbol_table::should_override(to, frombits, fromtype, OBJECT,
  340. object, &adjust_common_sizes,
  341. &adjust_dyndef, is_default_version))
  342. {
  343. elfcpp::STB tobinding = to->binding();
  344. typename Sized_symbol<size>::Value_type tovalue = to->value();
  345. this->override(to, sym, st_shndx, is_ordinary, object, version);
  346. if (adjust_common_sizes)
  347. {
  348. if (tosize > to->symsize())
  349. to->set_symsize(tosize);
  350. if (tovalue > to->value())
  351. to->set_value(tovalue);
  352. }
  353. if (adjust_dyndef)
  354. {
  355. // We are overriding an UNDEF or WEAK UNDEF with a DYN DEF.
  356. // Remember which kind of UNDEF it was for future reference.
  357. to->set_undef_binding(tobinding);
  358. }
  359. }
  360. else
  361. {
  362. if (adjust_common_sizes)
  363. {
  364. if (sym.get_st_size() > tosize)
  365. to->set_symsize(sym.get_st_size());
  366. if (sym.get_st_value() > to->value())
  367. to->set_value(sym.get_st_value());
  368. }
  369. if (adjust_dyndef)
  370. {
  371. // We are keeping a DYN DEF after seeing an UNDEF or WEAK UNDEF.
  372. // Remember which kind of UNDEF it was.
  373. to->set_undef_binding(sym.get_st_bind());
  374. }
  375. // The ELF ABI says that even for a reference to a symbol we
  376. // merge the visibility.
  377. to->override_visibility(sym.get_st_visibility());
  378. }
  379. if (adjust_common_sizes && parameters->options().warn_common())
  380. {
  381. if (tosize > sym.get_st_size())
  382. Symbol_table::report_resolve_problem(false,
  383. _("common of '%s' overriding "
  384. "smaller common"),
  385. to, OBJECT, object);
  386. else if (tosize < sym.get_st_size())
  387. Symbol_table::report_resolve_problem(false,
  388. _("common of '%s' overidden by "
  389. "larger common"),
  390. to, OBJECT, object);
  391. else
  392. Symbol_table::report_resolve_problem(false,
  393. _("multiple common of '%s'"),
  394. to, OBJECT, object);
  395. }
  396. }
  397. // Handle the core of symbol resolution. This is called with the
  398. // existing symbol, TO, and a bitflag describing the new symbol. This
  399. // returns true if we should override the existing symbol with the new
  400. // one, and returns false otherwise. It sets *ADJUST_COMMON_SIZES to
  401. // true if we should set the symbol size to the maximum of the TO and
  402. // FROM sizes. It handles error conditions.
  403. bool
  404. Symbol_table::should_override(const Symbol* to, unsigned int frombits,
  405. elfcpp::STT fromtype, Defined defined,
  406. Object* object, bool* adjust_common_sizes,
  407. bool* adjust_dyndef, bool is_default_version)
  408. {
  409. *adjust_common_sizes = false;
  410. *adjust_dyndef = false;
  411. unsigned int tobits;
  412. if (to->source() == Symbol::IS_UNDEFINED)
  413. tobits = symbol_to_bits(to->binding(), false, elfcpp::SHN_UNDEF, true);
  414. else if (to->source() != Symbol::FROM_OBJECT)
  415. tobits = symbol_to_bits(to->binding(), false, elfcpp::SHN_ABS, false);
  416. else
  417. {
  418. bool is_ordinary;
  419. unsigned int shndx = to->shndx(&is_ordinary);
  420. tobits = symbol_to_bits(to->binding(),
  421. to->object()->is_dynamic(),
  422. shndx,
  423. is_ordinary);
  424. }
  425. if ((to->type() == elfcpp::STT_TLS) ^ (fromtype == elfcpp::STT_TLS)
  426. && !to->is_placeholder())
  427. Symbol_table::report_resolve_problem(true,
  428. _("symbol '%s' used as both __thread "
  429. "and non-__thread"),
  430. to, defined, object);
  431. // We use a giant switch table for symbol resolution. This code is
  432. // unwieldy, but: 1) it is efficient; 2) we definitely handle all
  433. // cases; 3) it is easy to change the handling of a particular case.
  434. // The alternative would be a series of conditionals, but it is easy
  435. // to get the ordering wrong. This could also be done as a table,
  436. // but that is no easier to understand than this large switch
  437. // statement.
  438. // These are the values generated by the bit codes.
  439. enum
  440. {
  441. DEF = global_flag | regular_flag | def_flag,
  442. WEAK_DEF = weak_flag | regular_flag | def_flag,
  443. DYN_DEF = global_flag | dynamic_flag | def_flag,
  444. DYN_WEAK_DEF = weak_flag | dynamic_flag | def_flag,
  445. UNDEF = global_flag | regular_flag | undef_flag,
  446. WEAK_UNDEF = weak_flag | regular_flag | undef_flag,
  447. DYN_UNDEF = global_flag | dynamic_flag | undef_flag,
  448. DYN_WEAK_UNDEF = weak_flag | dynamic_flag | undef_flag,
  449. COMMON = global_flag | regular_flag | common_flag,
  450. WEAK_COMMON = weak_flag | regular_flag | common_flag,
  451. DYN_COMMON = global_flag | dynamic_flag | common_flag,
  452. DYN_WEAK_COMMON = weak_flag | dynamic_flag | common_flag
  453. };
  454. switch (tobits * 16 + frombits)
  455. {
  456. case DEF * 16 + DEF:
  457. // Two definitions of the same symbol.
  458. // If either symbol is defined by an object included using
  459. // --just-symbols, then don't warn. This is for compatibility
  460. // with the GNU linker. FIXME: This is a hack.
  461. if ((to->source() == Symbol::FROM_OBJECT && to->object()->just_symbols())
  462. || (object != NULL && object->just_symbols()))
  463. return false;
  464. if (!parameters->options().muldefs())
  465. Symbol_table::report_resolve_problem(true,
  466. _("multiple definition of '%s'"),
  467. to, defined, object);
  468. return false;
  469. case WEAK_DEF * 16 + DEF:
  470. // We've seen a weak definition, and now we see a strong
  471. // definition. In the original SVR4 linker, this was treated as
  472. // a multiple definition error. In the Solaris linker and the
  473. // GNU linker, a weak definition followed by a regular
  474. // definition causes the weak definition to be overridden. We
  475. // are currently compatible with the GNU linker. In the future
  476. // we should add a target specific option to change this.
  477. // FIXME.
  478. return true;
  479. case DYN_DEF * 16 + DEF:
  480. case DYN_WEAK_DEF * 16 + DEF:
  481. // We've seen a definition in a dynamic object, and now we see a
  482. // definition in a regular object. The definition in the
  483. // regular object overrides the definition in the dynamic
  484. // object.
  485. return true;
  486. case UNDEF * 16 + DEF:
  487. case WEAK_UNDEF * 16 + DEF:
  488. case DYN_UNDEF * 16 + DEF:
  489. case DYN_WEAK_UNDEF * 16 + DEF:
  490. // We've seen an undefined reference, and now we see a
  491. // definition. We use the definition.
  492. return true;
  493. case COMMON * 16 + DEF:
  494. case WEAK_COMMON * 16 + DEF:
  495. case DYN_COMMON * 16 + DEF:
  496. case DYN_WEAK_COMMON * 16 + DEF:
  497. // We've seen a common symbol and now we see a definition. The
  498. // definition overrides.
  499. if (parameters->options().warn_common())
  500. Symbol_table::report_resolve_problem(false,
  501. _("definition of '%s' overriding "
  502. "common"),
  503. to, defined, object);
  504. return true;
  505. case DEF * 16 + WEAK_DEF:
  506. case WEAK_DEF * 16 + WEAK_DEF:
  507. // We've seen a definition and now we see a weak definition. We
  508. // ignore the new weak definition.
  509. return false;
  510. case DYN_DEF * 16 + WEAK_DEF:
  511. case DYN_WEAK_DEF * 16 + WEAK_DEF:
  512. // We've seen a dynamic definition and now we see a regular weak
  513. // definition. The regular weak definition overrides.
  514. return true;
  515. case UNDEF * 16 + WEAK_DEF:
  516. case WEAK_UNDEF * 16 + WEAK_DEF:
  517. case DYN_UNDEF * 16 + WEAK_DEF:
  518. case DYN_WEAK_UNDEF * 16 + WEAK_DEF:
  519. // A weak definition of a currently undefined symbol.
  520. return true;
  521. case COMMON * 16 + WEAK_DEF:
  522. case WEAK_COMMON * 16 + WEAK_DEF:
  523. // A weak definition does not override a common definition.
  524. return false;
  525. case DYN_COMMON * 16 + WEAK_DEF:
  526. case DYN_WEAK_COMMON * 16 + WEAK_DEF:
  527. // A weak definition does override a definition in a dynamic
  528. // object.
  529. if (parameters->options().warn_common())
  530. Symbol_table::report_resolve_problem(false,
  531. _("definition of '%s' overriding "
  532. "dynamic common definition"),
  533. to, defined, object);
  534. return true;
  535. case DEF * 16 + DYN_DEF:
  536. case WEAK_DEF * 16 + DYN_DEF:
  537. // Ignore a dynamic definition if we already have a definition.
  538. return false;
  539. case DYN_DEF * 16 + DYN_DEF:
  540. case DYN_WEAK_DEF * 16 + DYN_DEF:
  541. // Ignore a dynamic definition if we already have a definition,
  542. // unless the existing definition is an unversioned definition
  543. // in the same dynamic object, and the new definition is a
  544. // default version.
  545. if (to->object() == object
  546. && to->version() == NULL
  547. && is_default_version)
  548. return true;
  549. return false;
  550. case UNDEF * 16 + DYN_DEF:
  551. case DYN_UNDEF * 16 + DYN_DEF:
  552. case DYN_WEAK_UNDEF * 16 + DYN_DEF:
  553. // Use a dynamic definition if we have a reference.
  554. return true;
  555. case WEAK_UNDEF * 16 + DYN_DEF:
  556. // When overriding a weak undef by a dynamic definition,
  557. // we need to remember that the original undef was weak.
  558. *adjust_dyndef = true;
  559. return true;
  560. case COMMON * 16 + DYN_DEF:
  561. case WEAK_COMMON * 16 + DYN_DEF:
  562. case DYN_COMMON * 16 + DYN_DEF:
  563. case DYN_WEAK_COMMON * 16 + DYN_DEF:
  564. // Ignore a dynamic definition if we already have a common
  565. // definition.
  566. return false;
  567. case DEF * 16 + DYN_WEAK_DEF:
  568. case WEAK_DEF * 16 + DYN_WEAK_DEF:
  569. case DYN_DEF * 16 + DYN_WEAK_DEF:
  570. case DYN_WEAK_DEF * 16 + DYN_WEAK_DEF:
  571. // Ignore a weak dynamic definition if we already have a
  572. // definition.
  573. return false;
  574. case UNDEF * 16 + DYN_WEAK_DEF:
  575. // When overriding an undef by a dynamic weak definition,
  576. // we need to remember that the original undef was not weak.
  577. *adjust_dyndef = true;
  578. return true;
  579. case DYN_UNDEF * 16 + DYN_WEAK_DEF:
  580. case DYN_WEAK_UNDEF * 16 + DYN_WEAK_DEF:
  581. // Use a weak dynamic definition if we have a reference.
  582. return true;
  583. case WEAK_UNDEF * 16 + DYN_WEAK_DEF:
  584. // When overriding a weak undef by a dynamic definition,
  585. // we need to remember that the original undef was weak.
  586. *adjust_dyndef = true;
  587. return true;
  588. case COMMON * 16 + DYN_WEAK_DEF:
  589. case WEAK_COMMON * 16 + DYN_WEAK_DEF:
  590. case DYN_COMMON * 16 + DYN_WEAK_DEF:
  591. case DYN_WEAK_COMMON * 16 + DYN_WEAK_DEF:
  592. // Ignore a weak dynamic definition if we already have a common
  593. // definition.
  594. return false;
  595. case DEF * 16 + UNDEF:
  596. case WEAK_DEF * 16 + UNDEF:
  597. case UNDEF * 16 + UNDEF:
  598. // A new undefined reference tells us nothing.
  599. return false;
  600. case DYN_DEF * 16 + UNDEF:
  601. case DYN_WEAK_DEF * 16 + UNDEF:
  602. // For a dynamic def, we need to remember which kind of undef we see.
  603. *adjust_dyndef = true;
  604. return false;
  605. case WEAK_UNDEF * 16 + UNDEF:
  606. case DYN_UNDEF * 16 + UNDEF:
  607. case DYN_WEAK_UNDEF * 16 + UNDEF:
  608. // A strong undef overrides a dynamic or weak undef.
  609. return true;
  610. case COMMON * 16 + UNDEF:
  611. case WEAK_COMMON * 16 + UNDEF:
  612. case DYN_COMMON * 16 + UNDEF:
  613. case DYN_WEAK_COMMON * 16 + UNDEF:
  614. // A new undefined reference tells us nothing.
  615. return false;
  616. case DEF * 16 + WEAK_UNDEF:
  617. case WEAK_DEF * 16 + WEAK_UNDEF:
  618. case UNDEF * 16 + WEAK_UNDEF:
  619. case WEAK_UNDEF * 16 + WEAK_UNDEF:
  620. case DYN_UNDEF * 16 + WEAK_UNDEF:
  621. case COMMON * 16 + WEAK_UNDEF:
  622. case WEAK_COMMON * 16 + WEAK_UNDEF:
  623. case DYN_COMMON * 16 + WEAK_UNDEF:
  624. case DYN_WEAK_COMMON * 16 + WEAK_UNDEF:
  625. // A new weak undefined reference tells us nothing unless the
  626. // exisiting symbol is a dynamic weak reference.
  627. return false;
  628. case DYN_WEAK_UNDEF * 16 + WEAK_UNDEF:
  629. // A new weak reference overrides an existing dynamic weak reference.
  630. // This is necessary because a dynamic weak reference remembers
  631. // the old binding, which may not be weak. If we keeps the existing
  632. // dynamic weak reference, the weakness may be dropped in the output.
  633. return true;
  634. case DYN_DEF * 16 + WEAK_UNDEF:
  635. case DYN_WEAK_DEF * 16 + WEAK_UNDEF:
  636. // For a dynamic def, we need to remember which kind of undef we see.
  637. *adjust_dyndef = true;
  638. return false;
  639. case DEF * 16 + DYN_UNDEF:
  640. case WEAK_DEF * 16 + DYN_UNDEF:
  641. case DYN_DEF * 16 + DYN_UNDEF:
  642. case DYN_WEAK_DEF * 16 + DYN_UNDEF:
  643. case UNDEF * 16 + DYN_UNDEF:
  644. case WEAK_UNDEF * 16 + DYN_UNDEF:
  645. case DYN_UNDEF * 16 + DYN_UNDEF:
  646. case DYN_WEAK_UNDEF * 16 + DYN_UNDEF:
  647. case COMMON * 16 + DYN_UNDEF:
  648. case WEAK_COMMON * 16 + DYN_UNDEF:
  649. case DYN_COMMON * 16 + DYN_UNDEF:
  650. case DYN_WEAK_COMMON * 16 + DYN_UNDEF:
  651. // A new dynamic undefined reference tells us nothing.
  652. return false;
  653. case DEF * 16 + DYN_WEAK_UNDEF:
  654. case WEAK_DEF * 16 + DYN_WEAK_UNDEF:
  655. case DYN_DEF * 16 + DYN_WEAK_UNDEF:
  656. case DYN_WEAK_DEF * 16 + DYN_WEAK_UNDEF:
  657. case UNDEF * 16 + DYN_WEAK_UNDEF:
  658. case WEAK_UNDEF * 16 + DYN_WEAK_UNDEF:
  659. case DYN_UNDEF * 16 + DYN_WEAK_UNDEF:
  660. case DYN_WEAK_UNDEF * 16 + DYN_WEAK_UNDEF:
  661. case COMMON * 16 + DYN_WEAK_UNDEF:
  662. case WEAK_COMMON * 16 + DYN_WEAK_UNDEF:
  663. case DYN_COMMON * 16 + DYN_WEAK_UNDEF:
  664. case DYN_WEAK_COMMON * 16 + DYN_WEAK_UNDEF:
  665. // A new weak dynamic undefined reference tells us nothing.
  666. return false;
  667. case DEF * 16 + COMMON:
  668. // A common symbol does not override a definition.
  669. if (parameters->options().warn_common())
  670. Symbol_table::report_resolve_problem(false,
  671. _("common '%s' overridden by "
  672. "previous definition"),
  673. to, defined, object);
  674. return false;
  675. case WEAK_DEF * 16 + COMMON:
  676. case DYN_DEF * 16 + COMMON:
  677. case DYN_WEAK_DEF * 16 + COMMON:
  678. // A common symbol does override a weak definition or a dynamic
  679. // definition.
  680. return true;
  681. case UNDEF * 16 + COMMON:
  682. case WEAK_UNDEF * 16 + COMMON:
  683. case DYN_UNDEF * 16 + COMMON:
  684. case DYN_WEAK_UNDEF * 16 + COMMON:
  685. // A common symbol is a definition for a reference.
  686. return true;
  687. case COMMON * 16 + COMMON:
  688. // Set the size to the maximum.
  689. *adjust_common_sizes = true;
  690. return false;
  691. case WEAK_COMMON * 16 + COMMON:
  692. // I'm not sure just what a weak common symbol means, but
  693. // presumably it can be overridden by a regular common symbol.
  694. return true;
  695. case DYN_COMMON * 16 + COMMON:
  696. case DYN_WEAK_COMMON * 16 + COMMON:
  697. // Use the real common symbol, but adjust the size if necessary.
  698. *adjust_common_sizes = true;
  699. return true;
  700. case DEF * 16 + WEAK_COMMON:
  701. case WEAK_DEF * 16 + WEAK_COMMON:
  702. case DYN_DEF * 16 + WEAK_COMMON:
  703. case DYN_WEAK_DEF * 16 + WEAK_COMMON:
  704. // Whatever a weak common symbol is, it won't override a
  705. // definition.
  706. return false;
  707. case UNDEF * 16 + WEAK_COMMON:
  708. case WEAK_UNDEF * 16 + WEAK_COMMON:
  709. case DYN_UNDEF * 16 + WEAK_COMMON:
  710. case DYN_WEAK_UNDEF * 16 + WEAK_COMMON:
  711. // A weak common symbol is better than an undefined symbol.
  712. return true;
  713. case COMMON * 16 + WEAK_COMMON:
  714. case WEAK_COMMON * 16 + WEAK_COMMON:
  715. case DYN_COMMON * 16 + WEAK_COMMON:
  716. case DYN_WEAK_COMMON * 16 + WEAK_COMMON:
  717. // Ignore a weak common symbol in the presence of a real common
  718. // symbol.
  719. return false;
  720. case DEF * 16 + DYN_COMMON:
  721. case WEAK_DEF * 16 + DYN_COMMON:
  722. case DYN_DEF * 16 + DYN_COMMON:
  723. case DYN_WEAK_DEF * 16 + DYN_COMMON:
  724. // Ignore a dynamic common symbol in the presence of a
  725. // definition.
  726. return false;
  727. case UNDEF * 16 + DYN_COMMON:
  728. case WEAK_UNDEF * 16 + DYN_COMMON:
  729. case DYN_UNDEF * 16 + DYN_COMMON:
  730. case DYN_WEAK_UNDEF * 16 + DYN_COMMON:
  731. // A dynamic common symbol is a definition of sorts.
  732. return true;
  733. case COMMON * 16 + DYN_COMMON:
  734. case WEAK_COMMON * 16 + DYN_COMMON:
  735. case DYN_COMMON * 16 + DYN_COMMON:
  736. case DYN_WEAK_COMMON * 16 + DYN_COMMON:
  737. // Set the size to the maximum.
  738. *adjust_common_sizes = true;
  739. return false;
  740. case DEF * 16 + DYN_WEAK_COMMON:
  741. case WEAK_DEF * 16 + DYN_WEAK_COMMON:
  742. case DYN_DEF * 16 + DYN_WEAK_COMMON:
  743. case DYN_WEAK_DEF * 16 + DYN_WEAK_COMMON:
  744. // A common symbol is ignored in the face of a definition.
  745. return false;
  746. case UNDEF * 16 + DYN_WEAK_COMMON:
  747. case WEAK_UNDEF * 16 + DYN_WEAK_COMMON:
  748. case DYN_UNDEF * 16 + DYN_WEAK_COMMON:
  749. case DYN_WEAK_UNDEF * 16 + DYN_WEAK_COMMON:
  750. // I guess a weak common symbol is better than a definition.
  751. return true;
  752. case COMMON * 16 + DYN_WEAK_COMMON:
  753. case WEAK_COMMON * 16 + DYN_WEAK_COMMON:
  754. case DYN_COMMON * 16 + DYN_WEAK_COMMON:
  755. case DYN_WEAK_COMMON * 16 + DYN_WEAK_COMMON:
  756. // Set the size to the maximum.
  757. *adjust_common_sizes = true;
  758. return false;
  759. default:
  760. gold_unreachable();
  761. }
  762. }
  763. // Issue an error or warning due to symbol resolution. IS_ERROR
  764. // indicates an error rather than a warning. MSG is the error
  765. // message; it is expected to have a %s for the symbol name. TO is
  766. // the existing symbol. DEFINED/OBJECT is where the new symbol was
  767. // found.
  768. // FIXME: We should have better location information here. When the
  769. // symbol is defined, we should be able to pull the location from the
  770. // debug info if there is any.
  771. void
  772. Symbol_table::report_resolve_problem(bool is_error, const char* msg,
  773. const Symbol* to, Defined defined,
  774. Object* object)
  775. {
  776. std::string demangled(to->demangled_name());
  777. size_t len = strlen(msg) + demangled.length() + 10;
  778. char* buf = new char[len];
  779. snprintf(buf, len, msg, demangled.c_str());
  780. const char* objname;
  781. switch (defined)
  782. {
  783. case OBJECT:
  784. objname = object->name().c_str();
  785. break;
  786. case COPY:
  787. objname = _("COPY reloc");
  788. break;
  789. case DEFSYM:
  790. case UNDEFINED:
  791. objname = _("command line");
  792. break;
  793. case SCRIPT:
  794. objname = _("linker script");
  795. break;
  796. case PREDEFINED:
  797. case INCREMENTAL_BASE:
  798. objname = _("linker defined");
  799. break;
  800. default:
  801. gold_unreachable();
  802. }
  803. if (is_error)
  804. gold_error("%s: %s", objname, buf);
  805. else
  806. gold_warning("%s: %s", objname, buf);
  807. delete[] buf;
  808. if (to->source() == Symbol::FROM_OBJECT)
  809. objname = to->object()->name().c_str();
  810. else
  811. objname = _("command line");
  812. gold_info("%s: %s: previous definition here", program_name, objname);
  813. }
  814. // A special case of should_override which is only called for a strong
  815. // defined symbol from a regular object file. This is used when
  816. // defining special symbols.
  817. bool
  818. Symbol_table::should_override_with_special(const Symbol* to,
  819. elfcpp::STT fromtype,
  820. Defined defined)
  821. {
  822. bool adjust_common_sizes;
  823. bool adjust_dyn_def;
  824. unsigned int frombits = global_flag | regular_flag | def_flag;
  825. bool ret = Symbol_table::should_override(to, frombits, fromtype, defined,
  826. NULL, &adjust_common_sizes,
  827. &adjust_dyn_def, false);
  828. gold_assert(!adjust_common_sizes && !adjust_dyn_def);
  829. return ret;
  830. }
  831. // Override symbol base with a special symbol.
  832. void
  833. Symbol::override_base_with_special(const Symbol* from)
  834. {
  835. bool same_name = this->name_ == from->name_;
  836. gold_assert(same_name || this->has_alias());
  837. // If we are overriding an undef, remember the original binding.
  838. if (this->is_undefined())
  839. this->set_undef_binding(this->binding_);
  840. this->source_ = from->source_;
  841. switch (from->source_)
  842. {
  843. case FROM_OBJECT:
  844. this->u_.from_object = from->u_.from_object;
  845. break;
  846. case IN_OUTPUT_DATA:
  847. this->u_.in_output_data = from->u_.in_output_data;
  848. break;
  849. case IN_OUTPUT_SEGMENT:
  850. this->u_.in_output_segment = from->u_.in_output_segment;
  851. break;
  852. case IS_CONSTANT:
  853. case IS_UNDEFINED:
  854. break;
  855. default:
  856. gold_unreachable();
  857. break;
  858. }
  859. if (same_name)
  860. {
  861. // When overriding a versioned symbol with a special symbol, we
  862. // may be changing the version. This will happen if we see a
  863. // special symbol such as "_end" defined in a shared object with
  864. // one version (from a version script), but we want to define it
  865. // here with a different version (from a different version
  866. // script).
  867. this->version_ = from->version_;
  868. }
  869. this->type_ = from->type_;
  870. this->binding_ = from->binding_;
  871. this->override_visibility(from->visibility_);
  872. this->nonvis_ = from->nonvis_;
  873. // Special symbols are always considered to be regular symbols.
  874. this->in_reg_ = true;
  875. if (from->needs_dynsym_entry_)
  876. this->needs_dynsym_entry_ = true;
  877. if (from->needs_dynsym_value_)
  878. this->needs_dynsym_value_ = true;
  879. this->is_predefined_ = from->is_predefined_;
  880. // We shouldn't see these flags. If we do, we need to handle them
  881. // somehow.
  882. gold_assert(!from->is_forwarder_);
  883. gold_assert(!from->has_plt_offset());
  884. gold_assert(!from->has_warning_);
  885. gold_assert(!from->is_copied_from_dynobj_);
  886. gold_assert(!from->is_forced_local_);
  887. }
  888. // Override a symbol with a special symbol.
  889. template<int size>
  890. void
  891. Sized_symbol<size>::override_with_special(const Sized_symbol<size>* from)
  892. {
  893. this->override_base_with_special(from);
  894. this->value_ = from->value_;
  895. this->symsize_ = from->symsize_;
  896. }
  897. // Override TOSYM with the special symbol FROMSYM. This handles all
  898. // aliases of TOSYM.
  899. template<int size>
  900. void
  901. Symbol_table::override_with_special(Sized_symbol<size>* tosym,
  902. const Sized_symbol<size>* fromsym)
  903. {
  904. tosym->override_with_special(fromsym);
  905. if (tosym->has_alias())
  906. {
  907. Symbol* sym = this->weak_aliases_[tosym];
  908. gold_assert(sym != NULL);
  909. Sized_symbol<size>* ssym = this->get_sized_symbol<size>(sym);
  910. do
  911. {
  912. ssym->override_with_special(fromsym);
  913. sym = this->weak_aliases_[ssym];
  914. gold_assert(sym != NULL);
  915. ssym = this->get_sized_symbol<size>(sym);
  916. }
  917. while (ssym != tosym);
  918. }
  919. if (tosym->binding() == elfcpp::STB_LOCAL
  920. || ((tosym->visibility() == elfcpp::STV_HIDDEN
  921. || tosym->visibility() == elfcpp::STV_INTERNAL)
  922. && (tosym->binding() == elfcpp::STB_GLOBAL
  923. || tosym->binding() == elfcpp::STB_GNU_UNIQUE
  924. || tosym->binding() == elfcpp::STB_WEAK)
  925. && !parameters->options().relocatable()))
  926. this->force_local(tosym);
  927. }
  928. // Instantiate the templates we need. We could use the configure
  929. // script to restrict this to only the ones needed for implemented
  930. // targets.
  931. // We have to instantiate both big and little endian versions because
  932. // these are used by other templates that depends on size only.
  933. #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
  934. template
  935. void
  936. Symbol_table::resolve<32, false>(
  937. Sized_symbol<32>* to,
  938. const elfcpp::Sym<32, false>& sym,
  939. unsigned int st_shndx,
  940. bool is_ordinary,
  941. unsigned int orig_st_shndx,
  942. Object* object,
  943. const char* version,
  944. bool is_default_version);
  945. template
  946. void
  947. Symbol_table::resolve<32, true>(
  948. Sized_symbol<32>* to,
  949. const elfcpp::Sym<32, true>& sym,
  950. unsigned int st_shndx,
  951. bool is_ordinary,
  952. unsigned int orig_st_shndx,
  953. Object* object,
  954. const char* version,
  955. bool is_default_version);
  956. #endif
  957. #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
  958. template
  959. void
  960. Symbol_table::resolve<64, false>(
  961. Sized_symbol<64>* to,
  962. const elfcpp::Sym<64, false>& sym,
  963. unsigned int st_shndx,
  964. bool is_ordinary,
  965. unsigned int orig_st_shndx,
  966. Object* object,
  967. const char* version,
  968. bool is_default_version);
  969. template
  970. void
  971. Symbol_table::resolve<64, true>(
  972. Sized_symbol<64>* to,
  973. const elfcpp::Sym<64, true>& sym,
  974. unsigned int st_shndx,
  975. bool is_ordinary,
  976. unsigned int orig_st_shndx,
  977. Object* object,
  978. const char* version,
  979. bool is_default_version);
  980. #endif
  981. #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
  982. template
  983. void
  984. Symbol_table::override_with_special<32>(Sized_symbol<32>*,
  985. const Sized_symbol<32>*);
  986. #endif
  987. #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
  988. template
  989. void
  990. Symbol_table::override_with_special<64>(Sized_symbol<64>*,
  991. const Sized_symbol<64>*);
  992. #endif
  993. } // End namespace gold.