object.cc 104 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454
  1. // object.cc -- support for an object file for linking in 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 <cerrno>
  19. #include <cstring>
  20. #include <cstdarg>
  21. #include "demangle.h"
  22. #include "libiberty.h"
  23. #include "gc.h"
  24. #include "target-select.h"
  25. #include "dwarf_reader.h"
  26. #include "layout.h"
  27. #include "output.h"
  28. #include "symtab.h"
  29. #include "cref.h"
  30. #include "reloc.h"
  31. #include "object.h"
  32. #include "dynobj.h"
  33. #include "plugin.h"
  34. #include "compressed_output.h"
  35. #include "incremental.h"
  36. #include "merge.h"
  37. namespace gold
  38. {
  39. // Struct Read_symbols_data.
  40. // Destroy any remaining File_view objects and buffers of decompressed
  41. // sections.
  42. Read_symbols_data::~Read_symbols_data()
  43. {
  44. if (this->section_headers != NULL)
  45. delete this->section_headers;
  46. if (this->section_names != NULL)
  47. delete this->section_names;
  48. if (this->symbols != NULL)
  49. delete this->symbols;
  50. if (this->symbol_names != NULL)
  51. delete this->symbol_names;
  52. if (this->versym != NULL)
  53. delete this->versym;
  54. if (this->verdef != NULL)
  55. delete this->verdef;
  56. if (this->verneed != NULL)
  57. delete this->verneed;
  58. }
  59. // Class Xindex.
  60. // Initialize the symtab_xindex_ array. Find the SHT_SYMTAB_SHNDX
  61. // section and read it in. SYMTAB_SHNDX is the index of the symbol
  62. // table we care about.
  63. template<int size, bool big_endian>
  64. void
  65. Xindex::initialize_symtab_xindex(Object* object, unsigned int symtab_shndx)
  66. {
  67. if (!this->symtab_xindex_.empty())
  68. return;
  69. gold_assert(symtab_shndx != 0);
  70. // Look through the sections in reverse order, on the theory that it
  71. // is more likely to be near the end than the beginning.
  72. unsigned int i = object->shnum();
  73. while (i > 0)
  74. {
  75. --i;
  76. if (object->section_type(i) == elfcpp::SHT_SYMTAB_SHNDX
  77. && this->adjust_shndx(object->section_link(i)) == symtab_shndx)
  78. {
  79. this->read_symtab_xindex<size, big_endian>(object, i, NULL);
  80. return;
  81. }
  82. }
  83. object->error(_("missing SHT_SYMTAB_SHNDX section"));
  84. }
  85. // Read in the symtab_xindex_ array, given the section index of the
  86. // SHT_SYMTAB_SHNDX section. If PSHDRS is not NULL, it points at the
  87. // section headers.
  88. template<int size, bool big_endian>
  89. void
  90. Xindex::read_symtab_xindex(Object* object, unsigned int xindex_shndx,
  91. const unsigned char* pshdrs)
  92. {
  93. section_size_type bytecount;
  94. const unsigned char* contents;
  95. if (pshdrs == NULL)
  96. contents = object->section_contents(xindex_shndx, &bytecount, false);
  97. else
  98. {
  99. const unsigned char* p = (pshdrs
  100. + (xindex_shndx
  101. * elfcpp::Elf_sizes<size>::shdr_size));
  102. typename elfcpp::Shdr<size, big_endian> shdr(p);
  103. bytecount = convert_to_section_size_type(shdr.get_sh_size());
  104. contents = object->get_view(shdr.get_sh_offset(), bytecount, true, false);
  105. }
  106. gold_assert(this->symtab_xindex_.empty());
  107. this->symtab_xindex_.reserve(bytecount / 4);
  108. for (section_size_type i = 0; i < bytecount; i += 4)
  109. {
  110. unsigned int shndx = elfcpp::Swap<32, big_endian>::readval(contents + i);
  111. // We preadjust the section indexes we save.
  112. this->symtab_xindex_.push_back(this->adjust_shndx(shndx));
  113. }
  114. }
  115. // Symbol symndx has a section of SHN_XINDEX; return the real section
  116. // index.
  117. unsigned int
  118. Xindex::sym_xindex_to_shndx(Object* object, unsigned int symndx)
  119. {
  120. if (symndx >= this->symtab_xindex_.size())
  121. {
  122. object->error(_("symbol %u out of range for SHT_SYMTAB_SHNDX section"),
  123. symndx);
  124. return elfcpp::SHN_UNDEF;
  125. }
  126. unsigned int shndx = this->symtab_xindex_[symndx];
  127. if (shndx < elfcpp::SHN_LORESERVE || shndx >= object->shnum())
  128. {
  129. object->error(_("extended index for symbol %u out of range: %u"),
  130. symndx, shndx);
  131. return elfcpp::SHN_UNDEF;
  132. }
  133. return shndx;
  134. }
  135. // Class Object.
  136. // Report an error for this object file. This is used by the
  137. // elfcpp::Elf_file interface, and also called by the Object code
  138. // itself.
  139. void
  140. Object::error(const char* format, ...) const
  141. {
  142. va_list args;
  143. va_start(args, format);
  144. char* buf = NULL;
  145. if (vasprintf(&buf, format, args) < 0)
  146. gold_nomem();
  147. va_end(args);
  148. gold_error(_("%s: %s"), this->name().c_str(), buf);
  149. free(buf);
  150. }
  151. // Return a view of the contents of a section.
  152. const unsigned char*
  153. Object::section_contents(unsigned int shndx, section_size_type* plen,
  154. bool cache)
  155. { return this->do_section_contents(shndx, plen, cache); }
  156. // Read the section data into SD. This is code common to Sized_relobj_file
  157. // and Sized_dynobj, so we put it into Object.
  158. template<int size, bool big_endian>
  159. void
  160. Object::read_section_data(elfcpp::Elf_file<size, big_endian, Object>* elf_file,
  161. Read_symbols_data* sd)
  162. {
  163. const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
  164. // Read the section headers.
  165. const off_t shoff = elf_file->shoff();
  166. const unsigned int shnum = this->shnum();
  167. sd->section_headers = this->get_lasting_view(shoff, shnum * shdr_size,
  168. true, true);
  169. // Read the section names.
  170. const unsigned char* pshdrs = sd->section_headers->data();
  171. const unsigned char* pshdrnames = pshdrs + elf_file->shstrndx() * shdr_size;
  172. typename elfcpp::Shdr<size, big_endian> shdrnames(pshdrnames);
  173. if (shdrnames.get_sh_type() != elfcpp::SHT_STRTAB)
  174. this->error(_("section name section has wrong type: %u"),
  175. static_cast<unsigned int>(shdrnames.get_sh_type()));
  176. sd->section_names_size =
  177. convert_to_section_size_type(shdrnames.get_sh_size());
  178. sd->section_names = this->get_lasting_view(shdrnames.get_sh_offset(),
  179. sd->section_names_size, false,
  180. false);
  181. }
  182. // If NAME is the name of a special .gnu.warning section, arrange for
  183. // the warning to be issued. SHNDX is the section index. Return
  184. // whether it is a warning section.
  185. bool
  186. Object::handle_gnu_warning_section(const char* name, unsigned int shndx,
  187. Symbol_table* symtab)
  188. {
  189. const char warn_prefix[] = ".gnu.warning.";
  190. const int warn_prefix_len = sizeof warn_prefix - 1;
  191. if (strncmp(name, warn_prefix, warn_prefix_len) == 0)
  192. {
  193. // Read the section contents to get the warning text. It would
  194. // be nicer if we only did this if we have to actually issue a
  195. // warning. Unfortunately, warnings are issued as we relocate
  196. // sections. That means that we can not lock the object then,
  197. // as we might try to issue the same warning multiple times
  198. // simultaneously.
  199. section_size_type len;
  200. const unsigned char* contents = this->section_contents(shndx, &len,
  201. false);
  202. if (len == 0)
  203. {
  204. const char* warning = name + warn_prefix_len;
  205. contents = reinterpret_cast<const unsigned char*>(warning);
  206. len = strlen(warning);
  207. }
  208. std::string warning(reinterpret_cast<const char*>(contents), len);
  209. symtab->add_warning(name + warn_prefix_len, this, warning);
  210. return true;
  211. }
  212. return false;
  213. }
  214. // If NAME is the name of the special section which indicates that
  215. // this object was compiled with -fsplit-stack, mark it accordingly.
  216. bool
  217. Object::handle_split_stack_section(const char* name)
  218. {
  219. if (strcmp(name, ".note.GNU-split-stack") == 0)
  220. {
  221. this->uses_split_stack_ = true;
  222. return true;
  223. }
  224. if (strcmp(name, ".note.GNU-no-split-stack") == 0)
  225. {
  226. this->has_no_split_stack_ = true;
  227. return true;
  228. }
  229. return false;
  230. }
  231. // Class Relobj
  232. template<int size>
  233. void
  234. Relobj::initialize_input_to_output_map(unsigned int shndx,
  235. typename elfcpp::Elf_types<size>::Elf_Addr starting_address,
  236. Unordered_map<section_offset_type,
  237. typename elfcpp::Elf_types<size>::Elf_Addr>* output_addresses) const {
  238. Object_merge_map *map = this->object_merge_map_;
  239. map->initialize_input_to_output_map<size>(shndx, starting_address,
  240. output_addresses);
  241. }
  242. void
  243. Relobj::add_merge_mapping(Output_section_data *output_data,
  244. unsigned int shndx, section_offset_type offset,
  245. section_size_type length,
  246. section_offset_type output_offset) {
  247. Object_merge_map* object_merge_map = this->get_or_create_merge_map();
  248. object_merge_map->add_mapping(output_data, shndx, offset, length, output_offset);
  249. }
  250. bool
  251. Relobj::merge_output_offset(unsigned int shndx, section_offset_type offset,
  252. section_offset_type *poutput) const {
  253. Object_merge_map* object_merge_map = this->object_merge_map_;
  254. if (object_merge_map == NULL)
  255. return false;
  256. return object_merge_map->get_output_offset(shndx, offset, poutput);
  257. }
  258. const Output_section_data*
  259. Relobj::find_merge_section(unsigned int shndx) const {
  260. Object_merge_map* object_merge_map = this->object_merge_map_;
  261. if (object_merge_map == NULL)
  262. return NULL;
  263. return object_merge_map->find_merge_section(shndx);
  264. }
  265. // To copy the symbols data read from the file to a local data structure.
  266. // This function is called from do_layout only while doing garbage
  267. // collection.
  268. void
  269. Relobj::copy_symbols_data(Symbols_data* gc_sd, Read_symbols_data* sd,
  270. unsigned int section_header_size)
  271. {
  272. gc_sd->section_headers_data =
  273. new unsigned char[(section_header_size)];
  274. memcpy(gc_sd->section_headers_data, sd->section_headers->data(),
  275. section_header_size);
  276. gc_sd->section_names_data =
  277. new unsigned char[sd->section_names_size];
  278. memcpy(gc_sd->section_names_data, sd->section_names->data(),
  279. sd->section_names_size);
  280. gc_sd->section_names_size = sd->section_names_size;
  281. if (sd->symbols != NULL)
  282. {
  283. gc_sd->symbols_data =
  284. new unsigned char[sd->symbols_size];
  285. memcpy(gc_sd->symbols_data, sd->symbols->data(),
  286. sd->symbols_size);
  287. }
  288. else
  289. {
  290. gc_sd->symbols_data = NULL;
  291. }
  292. gc_sd->symbols_size = sd->symbols_size;
  293. gc_sd->external_symbols_offset = sd->external_symbols_offset;
  294. if (sd->symbol_names != NULL)
  295. {
  296. gc_sd->symbol_names_data =
  297. new unsigned char[sd->symbol_names_size];
  298. memcpy(gc_sd->symbol_names_data, sd->symbol_names->data(),
  299. sd->symbol_names_size);
  300. }
  301. else
  302. {
  303. gc_sd->symbol_names_data = NULL;
  304. }
  305. gc_sd->symbol_names_size = sd->symbol_names_size;
  306. }
  307. // This function determines if a particular section name must be included
  308. // in the link. This is used during garbage collection to determine the
  309. // roots of the worklist.
  310. bool
  311. Relobj::is_section_name_included(const char* name)
  312. {
  313. if (is_prefix_of(".ctors", name)
  314. || is_prefix_of(".dtors", name)
  315. || is_prefix_of(".note", name)
  316. || is_prefix_of(".init", name)
  317. || is_prefix_of(".fini", name)
  318. || is_prefix_of(".gcc_except_table", name)
  319. || is_prefix_of(".jcr", name)
  320. || is_prefix_of(".preinit_array", name)
  321. || (is_prefix_of(".text", name)
  322. && strstr(name, "personality"))
  323. || (is_prefix_of(".data", name)
  324. && strstr(name, "personality"))
  325. || (is_prefix_of(".sdata", name)
  326. && strstr(name, "personality"))
  327. || (is_prefix_of(".gnu.linkonce.d", name)
  328. && strstr(name, "personality"))
  329. || (is_prefix_of(".rodata", name)
  330. && strstr(name, "nptl_version")))
  331. {
  332. return true;
  333. }
  334. return false;
  335. }
  336. // Finalize the incremental relocation information. Allocates a block
  337. // of relocation entries for each symbol, and sets the reloc_bases_
  338. // array to point to the first entry in each block. If CLEAR_COUNTS
  339. // is TRUE, also clear the per-symbol relocation counters.
  340. void
  341. Relobj::finalize_incremental_relocs(Layout* layout, bool clear_counts)
  342. {
  343. unsigned int nsyms = this->get_global_symbols()->size();
  344. this->reloc_bases_ = new unsigned int[nsyms];
  345. gold_assert(this->reloc_bases_ != NULL);
  346. gold_assert(layout->incremental_inputs() != NULL);
  347. unsigned int rindex = layout->incremental_inputs()->get_reloc_count();
  348. for (unsigned int i = 0; i < nsyms; ++i)
  349. {
  350. this->reloc_bases_[i] = rindex;
  351. rindex += this->reloc_counts_[i];
  352. if (clear_counts)
  353. this->reloc_counts_[i] = 0;
  354. }
  355. layout->incremental_inputs()->set_reloc_count(rindex);
  356. }
  357. Object_merge_map*
  358. Relobj::get_or_create_merge_map()
  359. {
  360. if (!this->object_merge_map_)
  361. this->object_merge_map_ = new Object_merge_map();
  362. return this->object_merge_map_;
  363. }
  364. // Class Sized_relobj.
  365. // Iterate over local symbols, calling a visitor class V for each GOT offset
  366. // associated with a local symbol.
  367. template<int size, bool big_endian>
  368. void
  369. Sized_relobj<size, big_endian>::do_for_all_local_got_entries(
  370. Got_offset_list::Visitor* v) const
  371. {
  372. unsigned int nsyms = this->local_symbol_count();
  373. for (unsigned int i = 0; i < nsyms; i++)
  374. {
  375. Local_got_offsets::const_iterator p = this->local_got_offsets_.find(i);
  376. if (p != this->local_got_offsets_.end())
  377. {
  378. const Got_offset_list* got_offsets = p->second;
  379. got_offsets->for_all_got_offsets(v);
  380. }
  381. }
  382. }
  383. // Get the address of an output section.
  384. template<int size, bool big_endian>
  385. uint64_t
  386. Sized_relobj<size, big_endian>::do_output_section_address(
  387. unsigned int shndx)
  388. {
  389. // If the input file is linked as --just-symbols, the output
  390. // section address is the input section address.
  391. if (this->just_symbols())
  392. return this->section_address(shndx);
  393. const Output_section* os = this->do_output_section(shndx);
  394. gold_assert(os != NULL);
  395. return os->address();
  396. }
  397. // Class Sized_relobj_file.
  398. template<int size, bool big_endian>
  399. Sized_relobj_file<size, big_endian>::Sized_relobj_file(
  400. const std::string& name,
  401. Input_file* input_file,
  402. off_t offset,
  403. const elfcpp::Ehdr<size, big_endian>& ehdr)
  404. : Sized_relobj<size, big_endian>(name, input_file, offset),
  405. elf_file_(this, ehdr),
  406. symtab_shndx_(-1U),
  407. local_symbol_count_(0),
  408. output_local_symbol_count_(0),
  409. output_local_dynsym_count_(0),
  410. symbols_(),
  411. defined_count_(0),
  412. local_symbol_offset_(0),
  413. local_dynsym_offset_(0),
  414. local_values_(),
  415. local_plt_offsets_(),
  416. kept_comdat_sections_(),
  417. has_eh_frame_(false),
  418. discarded_eh_frame_shndx_(-1U),
  419. is_deferred_layout_(false),
  420. deferred_layout_(),
  421. deferred_layout_relocs_()
  422. {
  423. this->e_type_ = ehdr.get_e_type();
  424. }
  425. template<int size, bool big_endian>
  426. Sized_relobj_file<size, big_endian>::~Sized_relobj_file()
  427. {
  428. }
  429. // Set up an object file based on the file header. This sets up the
  430. // section information.
  431. template<int size, bool big_endian>
  432. void
  433. Sized_relobj_file<size, big_endian>::do_setup()
  434. {
  435. const unsigned int shnum = this->elf_file_.shnum();
  436. this->set_shnum(shnum);
  437. }
  438. // Find the SHT_SYMTAB section, given the section headers. The ELF
  439. // standard says that maybe in the future there can be more than one
  440. // SHT_SYMTAB section. Until somebody figures out how that could
  441. // work, we assume there is only one.
  442. template<int size, bool big_endian>
  443. void
  444. Sized_relobj_file<size, big_endian>::find_symtab(const unsigned char* pshdrs)
  445. {
  446. const unsigned int shnum = this->shnum();
  447. this->symtab_shndx_ = 0;
  448. if (shnum > 0)
  449. {
  450. // Look through the sections in reverse order, since gas tends
  451. // to put the symbol table at the end.
  452. const unsigned char* p = pshdrs + shnum * This::shdr_size;
  453. unsigned int i = shnum;
  454. unsigned int xindex_shndx = 0;
  455. unsigned int xindex_link = 0;
  456. while (i > 0)
  457. {
  458. --i;
  459. p -= This::shdr_size;
  460. typename This::Shdr shdr(p);
  461. if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB)
  462. {
  463. this->symtab_shndx_ = i;
  464. if (xindex_shndx > 0 && xindex_link == i)
  465. {
  466. Xindex* xindex =
  467. new Xindex(this->elf_file_.large_shndx_offset());
  468. xindex->read_symtab_xindex<size, big_endian>(this,
  469. xindex_shndx,
  470. pshdrs);
  471. this->set_xindex(xindex);
  472. }
  473. break;
  474. }
  475. // Try to pick up the SHT_SYMTAB_SHNDX section, if there is
  476. // one. This will work if it follows the SHT_SYMTAB
  477. // section.
  478. if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB_SHNDX)
  479. {
  480. xindex_shndx = i;
  481. xindex_link = this->adjust_shndx(shdr.get_sh_link());
  482. }
  483. }
  484. }
  485. }
  486. // Return the Xindex structure to use for object with lots of
  487. // sections.
  488. template<int size, bool big_endian>
  489. Xindex*
  490. Sized_relobj_file<size, big_endian>::do_initialize_xindex()
  491. {
  492. gold_assert(this->symtab_shndx_ != -1U);
  493. Xindex* xindex = new Xindex(this->elf_file_.large_shndx_offset());
  494. xindex->initialize_symtab_xindex<size, big_endian>(this, this->symtab_shndx_);
  495. return xindex;
  496. }
  497. // Return whether SHDR has the right type and flags to be a GNU
  498. // .eh_frame section.
  499. template<int size, bool big_endian>
  500. bool
  501. Sized_relobj_file<size, big_endian>::check_eh_frame_flags(
  502. const elfcpp::Shdr<size, big_endian>* shdr) const
  503. {
  504. elfcpp::Elf_Word sh_type = shdr->get_sh_type();
  505. return ((sh_type == elfcpp::SHT_PROGBITS
  506. || sh_type == elfcpp::SHT_X86_64_UNWIND)
  507. && (shdr->get_sh_flags() & elfcpp::SHF_ALLOC) != 0);
  508. }
  509. // Find the section header with the given name.
  510. template<int size, bool big_endian>
  511. const unsigned char*
  512. Object::find_shdr(
  513. const unsigned char* pshdrs,
  514. const char* name,
  515. const char* names,
  516. section_size_type names_size,
  517. const unsigned char* hdr) const
  518. {
  519. const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
  520. const unsigned int shnum = this->shnum();
  521. const unsigned char* hdr_end = pshdrs + shdr_size * shnum;
  522. size_t sh_name = 0;
  523. while (1)
  524. {
  525. if (hdr)
  526. {
  527. // We found HDR last time we were called, continue looking.
  528. typename elfcpp::Shdr<size, big_endian> shdr(hdr);
  529. sh_name = shdr.get_sh_name();
  530. }
  531. else
  532. {
  533. // Look for the next occurrence of NAME in NAMES.
  534. // The fact that .shstrtab produced by current GNU tools is
  535. // string merged means we shouldn't have both .not.foo and
  536. // .foo in .shstrtab, and multiple .foo sections should all
  537. // have the same sh_name. However, this is not guaranteed
  538. // by the ELF spec and not all ELF object file producers may
  539. // be so clever.
  540. size_t len = strlen(name) + 1;
  541. const char *p = sh_name ? names + sh_name + len : names;
  542. p = reinterpret_cast<const char*>(memmem(p, names_size - (p - names),
  543. name, len));
  544. if (p == NULL)
  545. return NULL;
  546. sh_name = p - names;
  547. hdr = pshdrs;
  548. if (sh_name == 0)
  549. return hdr;
  550. }
  551. hdr += shdr_size;
  552. while (hdr < hdr_end)
  553. {
  554. typename elfcpp::Shdr<size, big_endian> shdr(hdr);
  555. if (shdr.get_sh_name() == sh_name)
  556. return hdr;
  557. hdr += shdr_size;
  558. }
  559. hdr = NULL;
  560. if (sh_name == 0)
  561. return hdr;
  562. }
  563. }
  564. // Return whether there is a GNU .eh_frame section, given the section
  565. // headers and the section names.
  566. template<int size, bool big_endian>
  567. bool
  568. Sized_relobj_file<size, big_endian>::find_eh_frame(
  569. const unsigned char* pshdrs,
  570. const char* names,
  571. section_size_type names_size) const
  572. {
  573. const unsigned char* s = NULL;
  574. while (1)
  575. {
  576. s = this->template find_shdr<size, big_endian>(pshdrs, ".eh_frame",
  577. names, names_size, s);
  578. if (s == NULL)
  579. return false;
  580. typename This::Shdr shdr(s);
  581. if (this->check_eh_frame_flags(&shdr))
  582. return true;
  583. }
  584. }
  585. // Return TRUE if this is a section whose contents will be needed in the
  586. // Add_symbols task. This function is only called for sections that have
  587. // already passed the test in is_compressed_debug_section() and the debug
  588. // section name prefix, ".debug"/".zdebug", has been skipped.
  589. static bool
  590. need_decompressed_section(const char* name)
  591. {
  592. if (*name++ != '_')
  593. return false;
  594. #ifdef ENABLE_THREADS
  595. // Decompressing these sections now will help only if we're
  596. // multithreaded.
  597. if (parameters->options().threads())
  598. {
  599. // We will need .zdebug_str if this is not an incremental link
  600. // (i.e., we are processing string merge sections) or if we need
  601. // to build a gdb index.
  602. if ((!parameters->incremental() || parameters->options().gdb_index())
  603. && strcmp(name, "str") == 0)
  604. return true;
  605. // We will need these other sections when building a gdb index.
  606. if (parameters->options().gdb_index()
  607. && (strcmp(name, "info") == 0
  608. || strcmp(name, "types") == 0
  609. || strcmp(name, "pubnames") == 0
  610. || strcmp(name, "pubtypes") == 0
  611. || strcmp(name, "ranges") == 0
  612. || strcmp(name, "abbrev") == 0))
  613. return true;
  614. }
  615. #endif
  616. // Even when single-threaded, we will need .zdebug_str if this is
  617. // not an incremental link and we are building a gdb index.
  618. // Otherwise, we would decompress the section twice: once for
  619. // string merge processing, and once for building the gdb index.
  620. if (!parameters->incremental()
  621. && parameters->options().gdb_index()
  622. && strcmp(name, "str") == 0)
  623. return true;
  624. return false;
  625. }
  626. // Build a table for any compressed debug sections, mapping each section index
  627. // to the uncompressed size and (if needed) the decompressed contents.
  628. template<int size, bool big_endian>
  629. Compressed_section_map*
  630. build_compressed_section_map(
  631. const unsigned char* pshdrs,
  632. unsigned int shnum,
  633. const char* names,
  634. section_size_type names_size,
  635. Object* obj,
  636. bool decompress_if_needed)
  637. {
  638. Compressed_section_map* uncompressed_map = new Compressed_section_map();
  639. const unsigned int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
  640. const unsigned char* p = pshdrs + shdr_size;
  641. for (unsigned int i = 1; i < shnum; ++i, p += shdr_size)
  642. {
  643. typename elfcpp::Shdr<size, big_endian> shdr(p);
  644. if (shdr.get_sh_type() == elfcpp::SHT_PROGBITS
  645. && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
  646. {
  647. if (shdr.get_sh_name() >= names_size)
  648. {
  649. obj->error(_("bad section name offset for section %u: %lu"),
  650. i, static_cast<unsigned long>(shdr.get_sh_name()));
  651. continue;
  652. }
  653. const char* name = names + shdr.get_sh_name();
  654. bool is_compressed = ((shdr.get_sh_flags()
  655. & elfcpp::SHF_COMPRESSED) != 0);
  656. bool is_zcompressed = (!is_compressed
  657. && is_compressed_debug_section(name));
  658. if (is_zcompressed || is_compressed)
  659. {
  660. section_size_type len;
  661. const unsigned char* contents =
  662. obj->section_contents(i, &len, false);
  663. uint64_t uncompressed_size;
  664. if (is_zcompressed)
  665. {
  666. // Skip over the ".zdebug" prefix.
  667. name += 7;
  668. uncompressed_size = get_uncompressed_size(contents, len);
  669. }
  670. else
  671. {
  672. // Skip over the ".debug" prefix.
  673. name += 6;
  674. elfcpp::Chdr<size, big_endian> chdr(contents);
  675. uncompressed_size = chdr.get_ch_size();
  676. }
  677. Compressed_section_info info;
  678. info.size = convert_to_section_size_type(uncompressed_size);
  679. info.flag = shdr.get_sh_flags();
  680. info.contents = NULL;
  681. if (uncompressed_size != -1ULL)
  682. {
  683. unsigned char* uncompressed_data = NULL;
  684. if (decompress_if_needed && need_decompressed_section(name))
  685. {
  686. uncompressed_data = new unsigned char[uncompressed_size];
  687. if (decompress_input_section(contents, len,
  688. uncompressed_data,
  689. uncompressed_size,
  690. size, big_endian,
  691. shdr.get_sh_flags()))
  692. info.contents = uncompressed_data;
  693. else
  694. delete[] uncompressed_data;
  695. }
  696. (*uncompressed_map)[i] = info;
  697. }
  698. }
  699. }
  700. }
  701. return uncompressed_map;
  702. }
  703. // Stash away info for a number of special sections.
  704. // Return true if any of the sections found require local symbols to be read.
  705. template<int size, bool big_endian>
  706. bool
  707. Sized_relobj_file<size, big_endian>::do_find_special_sections(
  708. Read_symbols_data* sd)
  709. {
  710. const unsigned char* const pshdrs = sd->section_headers->data();
  711. const unsigned char* namesu = sd->section_names->data();
  712. const char* names = reinterpret_cast<const char*>(namesu);
  713. if (this->find_eh_frame(pshdrs, names, sd->section_names_size))
  714. this->has_eh_frame_ = true;
  715. Compressed_section_map* compressed_sections =
  716. build_compressed_section_map<size, big_endian>(
  717. pshdrs, this->shnum(), names, sd->section_names_size, this, true);
  718. if (compressed_sections != NULL)
  719. this->set_compressed_sections(compressed_sections);
  720. return (this->has_eh_frame_
  721. || (!parameters->options().relocatable()
  722. && parameters->options().gdb_index()
  723. && (memmem(names, sd->section_names_size, "debug_info", 12) == 0
  724. || memmem(names, sd->section_names_size, "debug_types",
  725. 13) == 0)));
  726. }
  727. // Read the sections and symbols from an object file.
  728. template<int size, bool big_endian>
  729. void
  730. Sized_relobj_file<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
  731. {
  732. this->base_read_symbols(sd);
  733. }
  734. // Read the sections and symbols from an object file. This is common
  735. // code for all target-specific overrides of do_read_symbols().
  736. template<int size, bool big_endian>
  737. void
  738. Sized_relobj_file<size, big_endian>::base_read_symbols(Read_symbols_data* sd)
  739. {
  740. this->read_section_data(&this->elf_file_, sd);
  741. const unsigned char* const pshdrs = sd->section_headers->data();
  742. this->find_symtab(pshdrs);
  743. bool need_local_symbols = this->do_find_special_sections(sd);
  744. sd->symbols = NULL;
  745. sd->symbols_size = 0;
  746. sd->external_symbols_offset = 0;
  747. sd->symbol_names = NULL;
  748. sd->symbol_names_size = 0;
  749. if (this->symtab_shndx_ == 0)
  750. {
  751. // No symbol table. Weird but legal.
  752. return;
  753. }
  754. // Get the symbol table section header.
  755. typename This::Shdr symtabshdr(pshdrs
  756. + this->symtab_shndx_ * This::shdr_size);
  757. gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
  758. // If this object has a .eh_frame section, or if building a .gdb_index
  759. // section and there is debug info, we need all the symbols.
  760. // Otherwise we only need the external symbols. While it would be
  761. // simpler to just always read all the symbols, I've seen object
  762. // files with well over 2000 local symbols, which for a 64-bit
  763. // object file format is over 5 pages that we don't need to read
  764. // now.
  765. const int sym_size = This::sym_size;
  766. const unsigned int loccount = symtabshdr.get_sh_info();
  767. this->local_symbol_count_ = loccount;
  768. this->local_values_.resize(loccount);
  769. section_offset_type locsize = loccount * sym_size;
  770. off_t dataoff = symtabshdr.get_sh_offset();
  771. section_size_type datasize =
  772. convert_to_section_size_type(symtabshdr.get_sh_size());
  773. off_t extoff = dataoff + locsize;
  774. section_size_type extsize = datasize - locsize;
  775. off_t readoff = need_local_symbols ? dataoff : extoff;
  776. section_size_type readsize = need_local_symbols ? datasize : extsize;
  777. if (readsize == 0)
  778. {
  779. // No external symbols. Also weird but also legal.
  780. return;
  781. }
  782. File_view* fvsymtab = this->get_lasting_view(readoff, readsize, true, false);
  783. // Read the section header for the symbol names.
  784. unsigned int strtab_shndx = this->adjust_shndx(symtabshdr.get_sh_link());
  785. if (strtab_shndx >= this->shnum())
  786. {
  787. this->error(_("invalid symbol table name index: %u"), strtab_shndx);
  788. return;
  789. }
  790. typename This::Shdr strtabshdr(pshdrs + strtab_shndx * This::shdr_size);
  791. if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
  792. {
  793. this->error(_("symbol table name section has wrong type: %u"),
  794. static_cast<unsigned int>(strtabshdr.get_sh_type()));
  795. return;
  796. }
  797. // Read the symbol names.
  798. File_view* fvstrtab = this->get_lasting_view(strtabshdr.get_sh_offset(),
  799. strtabshdr.get_sh_size(),
  800. false, true);
  801. sd->symbols = fvsymtab;
  802. sd->symbols_size = readsize;
  803. sd->external_symbols_offset = need_local_symbols ? locsize : 0;
  804. sd->symbol_names = fvstrtab;
  805. sd->symbol_names_size =
  806. convert_to_section_size_type(strtabshdr.get_sh_size());
  807. }
  808. // Return the section index of symbol SYM. Set *VALUE to its value in
  809. // the object file. Set *IS_ORDINARY if this is an ordinary section
  810. // index, not a special code between SHN_LORESERVE and SHN_HIRESERVE.
  811. // Note that for a symbol which is not defined in this object file,
  812. // this will set *VALUE to 0 and return SHN_UNDEF; it will not return
  813. // the final value of the symbol in the link.
  814. template<int size, bool big_endian>
  815. unsigned int
  816. Sized_relobj_file<size, big_endian>::symbol_section_and_value(unsigned int sym,
  817. Address* value,
  818. bool* is_ordinary)
  819. {
  820. section_size_type symbols_size;
  821. const unsigned char* symbols = this->section_contents(this->symtab_shndx_,
  822. &symbols_size,
  823. false);
  824. const size_t count = symbols_size / This::sym_size;
  825. gold_assert(sym < count);
  826. elfcpp::Sym<size, big_endian> elfsym(symbols + sym * This::sym_size);
  827. *value = elfsym.get_st_value();
  828. return this->adjust_sym_shndx(sym, elfsym.get_st_shndx(), is_ordinary);
  829. }
  830. // Return whether to include a section group in the link. LAYOUT is
  831. // used to keep track of which section groups we have already seen.
  832. // INDEX is the index of the section group and SHDR is the section
  833. // header. If we do not want to include this group, we set bits in
  834. // OMIT for each section which should be discarded.
  835. template<int size, bool big_endian>
  836. bool
  837. Sized_relobj_file<size, big_endian>::include_section_group(
  838. Symbol_table* symtab,
  839. Layout* layout,
  840. unsigned int index,
  841. const char* name,
  842. const unsigned char* shdrs,
  843. const char* section_names,
  844. section_size_type section_names_size,
  845. std::vector<bool>* omit)
  846. {
  847. // Read the section contents.
  848. typename This::Shdr shdr(shdrs + index * This::shdr_size);
  849. const unsigned char* pcon = this->get_view(shdr.get_sh_offset(),
  850. shdr.get_sh_size(), true, false);
  851. const elfcpp::Elf_Word* pword =
  852. reinterpret_cast<const elfcpp::Elf_Word*>(pcon);
  853. // The first word contains flags. We only care about COMDAT section
  854. // groups. Other section groups are always included in the link
  855. // just like ordinary sections.
  856. elfcpp::Elf_Word flags = elfcpp::Swap<32, big_endian>::readval(pword);
  857. // Look up the group signature, which is the name of a symbol. ELF
  858. // uses a symbol name because some group signatures are long, and
  859. // the name is generally already in the symbol table, so it makes
  860. // sense to put the long string just once in .strtab rather than in
  861. // both .strtab and .shstrtab.
  862. // Get the appropriate symbol table header (this will normally be
  863. // the single SHT_SYMTAB section, but in principle it need not be).
  864. const unsigned int link = this->adjust_shndx(shdr.get_sh_link());
  865. typename This::Shdr symshdr(this, this->elf_file_.section_header(link));
  866. // Read the symbol table entry.
  867. unsigned int symndx = shdr.get_sh_info();
  868. if (symndx >= symshdr.get_sh_size() / This::sym_size)
  869. {
  870. this->error(_("section group %u info %u out of range"),
  871. index, symndx);
  872. return false;
  873. }
  874. off_t symoff = symshdr.get_sh_offset() + symndx * This::sym_size;
  875. const unsigned char* psym = this->get_view(symoff, This::sym_size, true,
  876. false);
  877. elfcpp::Sym<size, big_endian> sym(psym);
  878. // Read the symbol table names.
  879. section_size_type symnamelen;
  880. const unsigned char* psymnamesu;
  881. psymnamesu = this->section_contents(this->adjust_shndx(symshdr.get_sh_link()),
  882. &symnamelen, true);
  883. const char* psymnames = reinterpret_cast<const char*>(psymnamesu);
  884. // Get the section group signature.
  885. if (sym.get_st_name() >= symnamelen)
  886. {
  887. this->error(_("symbol %u name offset %u out of range"),
  888. symndx, sym.get_st_name());
  889. return false;
  890. }
  891. std::string signature(psymnames + sym.get_st_name());
  892. // It seems that some versions of gas will create a section group
  893. // associated with a section symbol, and then fail to give a name to
  894. // the section symbol. In such a case, use the name of the section.
  895. if (signature[0] == '\0' && sym.get_st_type() == elfcpp::STT_SECTION)
  896. {
  897. bool is_ordinary;
  898. unsigned int sym_shndx = this->adjust_sym_shndx(symndx,
  899. sym.get_st_shndx(),
  900. &is_ordinary);
  901. if (!is_ordinary || sym_shndx >= this->shnum())
  902. {
  903. this->error(_("symbol %u invalid section index %u"),
  904. symndx, sym_shndx);
  905. return false;
  906. }
  907. typename This::Shdr member_shdr(shdrs + sym_shndx * This::shdr_size);
  908. if (member_shdr.get_sh_name() < section_names_size)
  909. signature = section_names + member_shdr.get_sh_name();
  910. }
  911. // Record this section group in the layout, and see whether we've already
  912. // seen one with the same signature.
  913. bool include_group;
  914. bool is_comdat;
  915. Kept_section* kept_section = NULL;
  916. if ((flags & elfcpp::GRP_COMDAT) == 0)
  917. {
  918. include_group = true;
  919. is_comdat = false;
  920. }
  921. else
  922. {
  923. include_group = layout->find_or_add_kept_section(signature,
  924. this, index, true,
  925. true, &kept_section);
  926. is_comdat = true;
  927. }
  928. if (is_comdat && include_group)
  929. {
  930. Incremental_inputs* incremental_inputs = layout->incremental_inputs();
  931. if (incremental_inputs != NULL)
  932. incremental_inputs->report_comdat_group(this, signature.c_str());
  933. }
  934. size_t count = shdr.get_sh_size() / sizeof(elfcpp::Elf_Word);
  935. std::vector<unsigned int> shndxes;
  936. bool relocate_group = include_group && parameters->options().relocatable();
  937. if (relocate_group)
  938. shndxes.reserve(count - 1);
  939. for (size_t i = 1; i < count; ++i)
  940. {
  941. elfcpp::Elf_Word shndx =
  942. this->adjust_shndx(elfcpp::Swap<32, big_endian>::readval(pword + i));
  943. if (relocate_group)
  944. shndxes.push_back(shndx);
  945. if (shndx >= this->shnum())
  946. {
  947. this->error(_("section %u in section group %u out of range"),
  948. shndx, index);
  949. continue;
  950. }
  951. // Check for an earlier section number, since we're going to get
  952. // it wrong--we may have already decided to include the section.
  953. if (shndx < index)
  954. this->error(_("invalid section group %u refers to earlier section %u"),
  955. index, shndx);
  956. // Get the name of the member section.
  957. typename This::Shdr member_shdr(shdrs + shndx * This::shdr_size);
  958. if (member_shdr.get_sh_name() >= section_names_size)
  959. {
  960. // This is an error, but it will be diagnosed eventually
  961. // in do_layout, so we don't need to do anything here but
  962. // ignore it.
  963. continue;
  964. }
  965. std::string mname(section_names + member_shdr.get_sh_name());
  966. if (include_group)
  967. {
  968. if (is_comdat)
  969. kept_section->add_comdat_section(mname, shndx,
  970. member_shdr.get_sh_size());
  971. }
  972. else
  973. {
  974. (*omit)[shndx] = true;
  975. if (is_comdat)
  976. {
  977. Relobj* kept_object = kept_section->object();
  978. if (kept_section->is_comdat())
  979. {
  980. // Find the corresponding kept section, and store
  981. // that info in the discarded section table.
  982. unsigned int kept_shndx;
  983. uint64_t kept_size;
  984. if (kept_section->find_comdat_section(mname, &kept_shndx,
  985. &kept_size))
  986. {
  987. // We don't keep a mapping for this section if
  988. // it has a different size. The mapping is only
  989. // used for relocation processing, and we don't
  990. // want to treat the sections as similar if the
  991. // sizes are different. Checking the section
  992. // size is the approach used by the GNU linker.
  993. if (kept_size == member_shdr.get_sh_size())
  994. this->set_kept_comdat_section(shndx, kept_object,
  995. kept_shndx);
  996. }
  997. }
  998. else
  999. {
  1000. // The existing section is a linkonce section. Add
  1001. // a mapping if there is exactly one section in the
  1002. // group (which is true when COUNT == 2) and if it
  1003. // is the same size.
  1004. if (count == 2
  1005. && (kept_section->linkonce_size()
  1006. == member_shdr.get_sh_size()))
  1007. this->set_kept_comdat_section(shndx, kept_object,
  1008. kept_section->shndx());
  1009. }
  1010. }
  1011. }
  1012. }
  1013. if (relocate_group)
  1014. layout->layout_group(symtab, this, index, name, signature.c_str(),
  1015. shdr, flags, &shndxes);
  1016. return include_group;
  1017. }
  1018. // Whether to include a linkonce section in the link. NAME is the
  1019. // name of the section and SHDR is the section header.
  1020. // Linkonce sections are a GNU extension implemented in the original
  1021. // GNU linker before section groups were defined. The semantics are
  1022. // that we only include one linkonce section with a given name. The
  1023. // name of a linkonce section is normally .gnu.linkonce.T.SYMNAME,
  1024. // where T is the type of section and SYMNAME is the name of a symbol.
  1025. // In an attempt to make linkonce sections interact well with section
  1026. // groups, we try to identify SYMNAME and use it like a section group
  1027. // signature. We want to block section groups with that signature,
  1028. // but not other linkonce sections with that signature. We also use
  1029. // the full name of the linkonce section as a normal section group
  1030. // signature.
  1031. template<int size, bool big_endian>
  1032. bool
  1033. Sized_relobj_file<size, big_endian>::include_linkonce_section(
  1034. Layout* layout,
  1035. unsigned int index,
  1036. const char* name,
  1037. const elfcpp::Shdr<size, big_endian>& shdr)
  1038. {
  1039. typename elfcpp::Elf_types<size>::Elf_WXword sh_size = shdr.get_sh_size();
  1040. // In general the symbol name we want will be the string following
  1041. // the last '.'. However, we have to handle the case of
  1042. // .gnu.linkonce.t.__i686.get_pc_thunk.bx, which was generated by
  1043. // some versions of gcc. So we use a heuristic: if the name starts
  1044. // with ".gnu.linkonce.t.", we use everything after that. Otherwise
  1045. // we look for the last '.'. We can't always simply skip
  1046. // ".gnu.linkonce.X", because we have to deal with cases like
  1047. // ".gnu.linkonce.d.rel.ro.local".
  1048. const char* const linkonce_t = ".gnu.linkonce.t.";
  1049. const char* symname;
  1050. if (strncmp(name, linkonce_t, strlen(linkonce_t)) == 0)
  1051. symname = name + strlen(linkonce_t);
  1052. else
  1053. symname = strrchr(name, '.') + 1;
  1054. std::string sig1(symname);
  1055. std::string sig2(name);
  1056. Kept_section* kept1;
  1057. Kept_section* kept2;
  1058. bool include1 = layout->find_or_add_kept_section(sig1, this, index, false,
  1059. false, &kept1);
  1060. bool include2 = layout->find_or_add_kept_section(sig2, this, index, false,
  1061. true, &kept2);
  1062. if (!include2)
  1063. {
  1064. // We are not including this section because we already saw the
  1065. // name of the section as a signature. This normally implies
  1066. // that the kept section is another linkonce section. If it is
  1067. // the same size, record it as the section which corresponds to
  1068. // this one.
  1069. if (kept2->object() != NULL
  1070. && !kept2->is_comdat()
  1071. && kept2->linkonce_size() == sh_size)
  1072. this->set_kept_comdat_section(index, kept2->object(), kept2->shndx());
  1073. }
  1074. else if (!include1)
  1075. {
  1076. // The section is being discarded on the basis of its symbol
  1077. // name. This means that the corresponding kept section was
  1078. // part of a comdat group, and it will be difficult to identify
  1079. // the specific section within that group that corresponds to
  1080. // this linkonce section. We'll handle the simple case where
  1081. // the group has only one member section. Otherwise, it's not
  1082. // worth the effort.
  1083. unsigned int kept_shndx;
  1084. uint64_t kept_size;
  1085. if (kept1->object() != NULL
  1086. && kept1->is_comdat()
  1087. && kept1->find_single_comdat_section(&kept_shndx, &kept_size)
  1088. && kept_size == sh_size)
  1089. this->set_kept_comdat_section(index, kept1->object(), kept_shndx);
  1090. }
  1091. else
  1092. {
  1093. kept1->set_linkonce_size(sh_size);
  1094. kept2->set_linkonce_size(sh_size);
  1095. }
  1096. return include1 && include2;
  1097. }
  1098. // Layout an input section.
  1099. template<int size, bool big_endian>
  1100. inline void
  1101. Sized_relobj_file<size, big_endian>::layout_section(
  1102. Layout* layout,
  1103. unsigned int shndx,
  1104. const char* name,
  1105. const typename This::Shdr& shdr,
  1106. unsigned int reloc_shndx,
  1107. unsigned int reloc_type)
  1108. {
  1109. off_t offset;
  1110. Output_section* os = layout->layout(this, shndx, name, shdr,
  1111. reloc_shndx, reloc_type, &offset);
  1112. this->output_sections()[shndx] = os;
  1113. if (offset == -1)
  1114. this->section_offsets()[shndx] = invalid_address;
  1115. else
  1116. this->section_offsets()[shndx] = convert_types<Address, off_t>(offset);
  1117. // If this section requires special handling, and if there are
  1118. // relocs that apply to it, then we must do the special handling
  1119. // before we apply the relocs.
  1120. if (offset == -1 && reloc_shndx != 0)
  1121. this->set_relocs_must_follow_section_writes();
  1122. }
  1123. // Layout an input .eh_frame section.
  1124. template<int size, bool big_endian>
  1125. void
  1126. Sized_relobj_file<size, big_endian>::layout_eh_frame_section(
  1127. Layout* layout,
  1128. const unsigned char* symbols_data,
  1129. section_size_type symbols_size,
  1130. const unsigned char* symbol_names_data,
  1131. section_size_type symbol_names_size,
  1132. unsigned int shndx,
  1133. const typename This::Shdr& shdr,
  1134. unsigned int reloc_shndx,
  1135. unsigned int reloc_type)
  1136. {
  1137. gold_assert(this->has_eh_frame_);
  1138. off_t offset;
  1139. Output_section* os = layout->layout_eh_frame(this,
  1140. symbols_data,
  1141. symbols_size,
  1142. symbol_names_data,
  1143. symbol_names_size,
  1144. shndx,
  1145. shdr,
  1146. reloc_shndx,
  1147. reloc_type,
  1148. &offset);
  1149. this->output_sections()[shndx] = os;
  1150. if (os == NULL || offset == -1)
  1151. {
  1152. // An object can contain at most one section holding exception
  1153. // frame information.
  1154. gold_assert(this->discarded_eh_frame_shndx_ == -1U);
  1155. this->discarded_eh_frame_shndx_ = shndx;
  1156. this->section_offsets()[shndx] = invalid_address;
  1157. }
  1158. else
  1159. this->section_offsets()[shndx] = convert_types<Address, off_t>(offset);
  1160. // If this section requires special handling, and if there are
  1161. // relocs that aply to it, then we must do the special handling
  1162. // before we apply the relocs.
  1163. if (os != NULL && offset == -1 && reloc_shndx != 0)
  1164. this->set_relocs_must_follow_section_writes();
  1165. }
  1166. // Lay out the input sections. We walk through the sections and check
  1167. // whether they should be included in the link. If they should, we
  1168. // pass them to the Layout object, which will return an output section
  1169. // and an offset.
  1170. // This function is called twice sometimes, two passes, when mapping
  1171. // of input sections to output sections must be delayed.
  1172. // This is true for the following :
  1173. // * Garbage collection (--gc-sections): Some input sections will be
  1174. // discarded and hence the assignment must wait until the second pass.
  1175. // In the first pass, it is for setting up some sections as roots to
  1176. // a work-list for --gc-sections and to do comdat processing.
  1177. // * Identical Code Folding (--icf=<safe,all>): Some input sections
  1178. // will be folded and hence the assignment must wait.
  1179. // * Using plugins to map some sections to unique segments: Mapping
  1180. // some sections to unique segments requires mapping them to unique
  1181. // output sections too. This can be done via plugins now and this
  1182. // information is not available in the first pass.
  1183. template<int size, bool big_endian>
  1184. void
  1185. Sized_relobj_file<size, big_endian>::do_layout(Symbol_table* symtab,
  1186. Layout* layout,
  1187. Read_symbols_data* sd)
  1188. {
  1189. const unsigned int shnum = this->shnum();
  1190. /* Should this function be called twice? */
  1191. bool is_two_pass = (parameters->options().gc_sections()
  1192. || parameters->options().icf_enabled()
  1193. || layout->is_unique_segment_for_sections_specified());
  1194. /* Only one of is_pass_one and is_pass_two is true. Both are false when
  1195. a two-pass approach is not needed. */
  1196. bool is_pass_one = false;
  1197. bool is_pass_two = false;
  1198. Symbols_data* gc_sd = NULL;
  1199. /* Check if do_layout needs to be two-pass. If so, find out which pass
  1200. should happen. In the first pass, the data in sd is saved to be used
  1201. later in the second pass. */
  1202. if (is_two_pass)
  1203. {
  1204. gc_sd = this->get_symbols_data();
  1205. if (gc_sd == NULL)
  1206. {
  1207. gold_assert(sd != NULL);
  1208. is_pass_one = true;
  1209. }
  1210. else
  1211. {
  1212. if (parameters->options().gc_sections())
  1213. gold_assert(symtab->gc()->is_worklist_ready());
  1214. if (parameters->options().icf_enabled())
  1215. gold_assert(symtab->icf()->is_icf_ready());
  1216. is_pass_two = true;
  1217. }
  1218. }
  1219. if (shnum == 0)
  1220. return;
  1221. if (is_pass_one)
  1222. {
  1223. // During garbage collection save the symbols data to use it when
  1224. // re-entering this function.
  1225. gc_sd = new Symbols_data;
  1226. this->copy_symbols_data(gc_sd, sd, This::shdr_size * shnum);
  1227. this->set_symbols_data(gc_sd);
  1228. }
  1229. const unsigned char* section_headers_data = NULL;
  1230. section_size_type section_names_size;
  1231. const unsigned char* symbols_data = NULL;
  1232. section_size_type symbols_size;
  1233. const unsigned char* symbol_names_data = NULL;
  1234. section_size_type symbol_names_size;
  1235. if (is_two_pass)
  1236. {
  1237. section_headers_data = gc_sd->section_headers_data;
  1238. section_names_size = gc_sd->section_names_size;
  1239. symbols_data = gc_sd->symbols_data;
  1240. symbols_size = gc_sd->symbols_size;
  1241. symbol_names_data = gc_sd->symbol_names_data;
  1242. symbol_names_size = gc_sd->symbol_names_size;
  1243. }
  1244. else
  1245. {
  1246. section_headers_data = sd->section_headers->data();
  1247. section_names_size = sd->section_names_size;
  1248. if (sd->symbols != NULL)
  1249. symbols_data = sd->symbols->data();
  1250. symbols_size = sd->symbols_size;
  1251. if (sd->symbol_names != NULL)
  1252. symbol_names_data = sd->symbol_names->data();
  1253. symbol_names_size = sd->symbol_names_size;
  1254. }
  1255. // Get the section headers.
  1256. const unsigned char* shdrs = section_headers_data;
  1257. const unsigned char* pshdrs;
  1258. // Get the section names.
  1259. const unsigned char* pnamesu = (is_two_pass
  1260. ? gc_sd->section_names_data
  1261. : sd->section_names->data());
  1262. const char* pnames = reinterpret_cast<const char*>(pnamesu);
  1263. // If any input files have been claimed by plugins, we need to defer
  1264. // actual layout until the replacement files have arrived.
  1265. const bool should_defer_layout =
  1266. (parameters->options().has_plugins()
  1267. && parameters->options().plugins()->should_defer_layout());
  1268. unsigned int num_sections_to_defer = 0;
  1269. // For each section, record the index of the reloc section if any.
  1270. // Use 0 to mean that there is no reloc section, -1U to mean that
  1271. // there is more than one.
  1272. std::vector<unsigned int> reloc_shndx(shnum, 0);
  1273. std::vector<unsigned int> reloc_type(shnum, elfcpp::SHT_NULL);
  1274. // Skip the first, dummy, section.
  1275. pshdrs = shdrs + This::shdr_size;
  1276. for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size)
  1277. {
  1278. typename This::Shdr shdr(pshdrs);
  1279. // Count the number of sections whose layout will be deferred.
  1280. if (should_defer_layout && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC))
  1281. ++num_sections_to_defer;
  1282. unsigned int sh_type = shdr.get_sh_type();
  1283. if (sh_type == elfcpp::SHT_REL || sh_type == elfcpp::SHT_RELA)
  1284. {
  1285. unsigned int target_shndx = this->adjust_shndx(shdr.get_sh_info());
  1286. if (target_shndx == 0 || target_shndx >= shnum)
  1287. {
  1288. this->error(_("relocation section %u has bad info %u"),
  1289. i, target_shndx);
  1290. continue;
  1291. }
  1292. if (reloc_shndx[target_shndx] != 0)
  1293. reloc_shndx[target_shndx] = -1U;
  1294. else
  1295. {
  1296. reloc_shndx[target_shndx] = i;
  1297. reloc_type[target_shndx] = sh_type;
  1298. }
  1299. }
  1300. }
  1301. Output_sections& out_sections(this->output_sections());
  1302. std::vector<Address>& out_section_offsets(this->section_offsets());
  1303. if (!is_pass_two)
  1304. {
  1305. out_sections.resize(shnum);
  1306. out_section_offsets.resize(shnum);
  1307. }
  1308. // If we are only linking for symbols, then there is nothing else to
  1309. // do here.
  1310. if (this->input_file()->just_symbols())
  1311. {
  1312. if (!is_pass_two)
  1313. {
  1314. delete sd->section_headers;
  1315. sd->section_headers = NULL;
  1316. delete sd->section_names;
  1317. sd->section_names = NULL;
  1318. }
  1319. return;
  1320. }
  1321. if (num_sections_to_defer > 0)
  1322. {
  1323. parameters->options().plugins()->add_deferred_layout_object(this);
  1324. this->deferred_layout_.reserve(num_sections_to_defer);
  1325. this->is_deferred_layout_ = true;
  1326. }
  1327. // Whether we've seen a .note.GNU-stack section.
  1328. bool seen_gnu_stack = false;
  1329. // The flags of a .note.GNU-stack section.
  1330. uint64_t gnu_stack_flags = 0;
  1331. // Keep track of which sections to omit.
  1332. std::vector<bool> omit(shnum, false);
  1333. // Keep track of reloc sections when emitting relocations.
  1334. const bool relocatable = parameters->options().relocatable();
  1335. const bool emit_relocs = (relocatable
  1336. || parameters->options().emit_relocs());
  1337. std::vector<unsigned int> reloc_sections;
  1338. // Keep track of .eh_frame sections.
  1339. std::vector<unsigned int> eh_frame_sections;
  1340. // Keep track of .debug_info and .debug_types sections.
  1341. std::vector<unsigned int> debug_info_sections;
  1342. std::vector<unsigned int> debug_types_sections;
  1343. // Skip the first, dummy, section.
  1344. pshdrs = shdrs + This::shdr_size;
  1345. for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size)
  1346. {
  1347. typename This::Shdr shdr(pshdrs);
  1348. if (shdr.get_sh_name() >= section_names_size)
  1349. {
  1350. this->error(_("bad section name offset for section %u: %lu"),
  1351. i, static_cast<unsigned long>(shdr.get_sh_name()));
  1352. return;
  1353. }
  1354. const char* name = pnames + shdr.get_sh_name();
  1355. if (!is_pass_two)
  1356. {
  1357. if (this->handle_gnu_warning_section(name, i, symtab))
  1358. {
  1359. if (!relocatable && !parameters->options().shared())
  1360. omit[i] = true;
  1361. }
  1362. // The .note.GNU-stack section is special. It gives the
  1363. // protection flags that this object file requires for the stack
  1364. // in memory.
  1365. if (strcmp(name, ".note.GNU-stack") == 0)
  1366. {
  1367. seen_gnu_stack = true;
  1368. gnu_stack_flags |= shdr.get_sh_flags();
  1369. omit[i] = true;
  1370. }
  1371. // The .note.GNU-split-stack section is also special. It
  1372. // indicates that the object was compiled with
  1373. // -fsplit-stack.
  1374. if (this->handle_split_stack_section(name))
  1375. {
  1376. if (!relocatable && !parameters->options().shared())
  1377. omit[i] = true;
  1378. }
  1379. // Skip attributes section.
  1380. if (parameters->target().is_attributes_section(name))
  1381. {
  1382. omit[i] = true;
  1383. }
  1384. bool discard = omit[i];
  1385. if (!discard)
  1386. {
  1387. if (shdr.get_sh_type() == elfcpp::SHT_GROUP)
  1388. {
  1389. if (!this->include_section_group(symtab, layout, i, name,
  1390. shdrs, pnames,
  1391. section_names_size,
  1392. &omit))
  1393. discard = true;
  1394. }
  1395. else if ((shdr.get_sh_flags() & elfcpp::SHF_GROUP) == 0
  1396. && Layout::is_linkonce(name))
  1397. {
  1398. if (!this->include_linkonce_section(layout, i, name, shdr))
  1399. discard = true;
  1400. }
  1401. }
  1402. // Add the section to the incremental inputs layout.
  1403. Incremental_inputs* incremental_inputs = layout->incremental_inputs();
  1404. if (incremental_inputs != NULL
  1405. && !discard
  1406. && can_incremental_update(shdr.get_sh_type()))
  1407. {
  1408. off_t sh_size = shdr.get_sh_size();
  1409. section_size_type uncompressed_size;
  1410. if (this->section_is_compressed(i, &uncompressed_size))
  1411. sh_size = uncompressed_size;
  1412. incremental_inputs->report_input_section(this, i, name, sh_size);
  1413. }
  1414. if (discard)
  1415. {
  1416. // Do not include this section in the link.
  1417. out_sections[i] = NULL;
  1418. out_section_offsets[i] = invalid_address;
  1419. continue;
  1420. }
  1421. }
  1422. if (is_pass_one && parameters->options().gc_sections())
  1423. {
  1424. if (this->is_section_name_included(name)
  1425. || layout->keep_input_section (this, name)
  1426. || shdr.get_sh_type() == elfcpp::SHT_INIT_ARRAY
  1427. || shdr.get_sh_type() == elfcpp::SHT_FINI_ARRAY)
  1428. {
  1429. symtab->gc()->worklist().push_back(Section_id(this, i));
  1430. }
  1431. // If the section name XXX can be represented as a C identifier
  1432. // it cannot be discarded if there are references to
  1433. // __start_XXX and __stop_XXX symbols. These need to be
  1434. // specially handled.
  1435. if (is_cident(name))
  1436. {
  1437. symtab->gc()->add_cident_section(name, Section_id(this, i));
  1438. }
  1439. }
  1440. // When doing a relocatable link we are going to copy input
  1441. // reloc sections into the output. We only want to copy the
  1442. // ones associated with sections which are not being discarded.
  1443. // However, we don't know that yet for all sections. So save
  1444. // reloc sections and process them later. Garbage collection is
  1445. // not triggered when relocatable code is desired.
  1446. if (emit_relocs
  1447. && (shdr.get_sh_type() == elfcpp::SHT_REL
  1448. || shdr.get_sh_type() == elfcpp::SHT_RELA))
  1449. {
  1450. reloc_sections.push_back(i);
  1451. continue;
  1452. }
  1453. if (relocatable && shdr.get_sh_type() == elfcpp::SHT_GROUP)
  1454. continue;
  1455. // The .eh_frame section is special. It holds exception frame
  1456. // information that we need to read in order to generate the
  1457. // exception frame header. We process these after all the other
  1458. // sections so that the exception frame reader can reliably
  1459. // determine which sections are being discarded, and discard the
  1460. // corresponding information.
  1461. if (!relocatable
  1462. && strcmp(name, ".eh_frame") == 0
  1463. && this->check_eh_frame_flags(&shdr))
  1464. {
  1465. if (is_pass_one)
  1466. {
  1467. if (this->is_deferred_layout())
  1468. out_sections[i] = reinterpret_cast<Output_section*>(2);
  1469. else
  1470. out_sections[i] = reinterpret_cast<Output_section*>(1);
  1471. out_section_offsets[i] = invalid_address;
  1472. }
  1473. else if (this->is_deferred_layout())
  1474. this->deferred_layout_.push_back(Deferred_layout(i, name,
  1475. pshdrs,
  1476. reloc_shndx[i],
  1477. reloc_type[i]));
  1478. else
  1479. eh_frame_sections.push_back(i);
  1480. continue;
  1481. }
  1482. if (is_pass_two && parameters->options().gc_sections())
  1483. {
  1484. // This is executed during the second pass of garbage
  1485. // collection. do_layout has been called before and some
  1486. // sections have been already discarded. Simply ignore
  1487. // such sections this time around.
  1488. if (out_sections[i] == NULL)
  1489. {
  1490. gold_assert(out_section_offsets[i] == invalid_address);
  1491. continue;
  1492. }
  1493. if (((shdr.get_sh_flags() & elfcpp::SHF_ALLOC) != 0)
  1494. && symtab->gc()->is_section_garbage(this, i))
  1495. {
  1496. if (parameters->options().print_gc_sections())
  1497. gold_info(_("%s: removing unused section from '%s'"
  1498. " in file '%s'"),
  1499. program_name, this->section_name(i).c_str(),
  1500. this->name().c_str());
  1501. out_sections[i] = NULL;
  1502. out_section_offsets[i] = invalid_address;
  1503. continue;
  1504. }
  1505. }
  1506. if (is_pass_two && parameters->options().icf_enabled())
  1507. {
  1508. if (out_sections[i] == NULL)
  1509. {
  1510. gold_assert(out_section_offsets[i] == invalid_address);
  1511. continue;
  1512. }
  1513. if (((shdr.get_sh_flags() & elfcpp::SHF_ALLOC) != 0)
  1514. && symtab->icf()->is_section_folded(this, i))
  1515. {
  1516. if (parameters->options().print_icf_sections())
  1517. {
  1518. Section_id folded =
  1519. symtab->icf()->get_folded_section(this, i);
  1520. Relobj* folded_obj =
  1521. reinterpret_cast<Relobj*>(folded.first);
  1522. gold_info(_("%s: ICF folding section '%s' in file '%s' "
  1523. "into '%s' in file '%s'"),
  1524. program_name, this->section_name(i).c_str(),
  1525. this->name().c_str(),
  1526. folded_obj->section_name(folded.second).c_str(),
  1527. folded_obj->name().c_str());
  1528. }
  1529. out_sections[i] = NULL;
  1530. out_section_offsets[i] = invalid_address;
  1531. continue;
  1532. }
  1533. }
  1534. // Defer layout here if input files are claimed by plugins. When gc
  1535. // is turned on this function is called twice; we only want to do this
  1536. // on the first pass.
  1537. if (!is_pass_two
  1538. && this->is_deferred_layout()
  1539. && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC))
  1540. {
  1541. this->deferred_layout_.push_back(Deferred_layout(i, name,
  1542. pshdrs,
  1543. reloc_shndx[i],
  1544. reloc_type[i]));
  1545. // Put dummy values here; real values will be supplied by
  1546. // do_layout_deferred_sections.
  1547. out_sections[i] = reinterpret_cast<Output_section*>(2);
  1548. out_section_offsets[i] = invalid_address;
  1549. continue;
  1550. }
  1551. // During gc_pass_two if a section that was previously deferred is
  1552. // found, do not layout the section as layout_deferred_sections will
  1553. // do it later from gold.cc.
  1554. if (is_pass_two
  1555. && (out_sections[i] == reinterpret_cast<Output_section*>(2)))
  1556. continue;
  1557. if (is_pass_one)
  1558. {
  1559. // This is during garbage collection. The out_sections are
  1560. // assigned in the second call to this function.
  1561. out_sections[i] = reinterpret_cast<Output_section*>(1);
  1562. out_section_offsets[i] = invalid_address;
  1563. }
  1564. else
  1565. {
  1566. // When garbage collection is switched on the actual layout
  1567. // only happens in the second call.
  1568. this->layout_section(layout, i, name, shdr, reloc_shndx[i],
  1569. reloc_type[i]);
  1570. // When generating a .gdb_index section, we do additional
  1571. // processing of .debug_info and .debug_types sections after all
  1572. // the other sections for the same reason as above.
  1573. if (!relocatable
  1574. && parameters->options().gdb_index()
  1575. && !(shdr.get_sh_flags() & elfcpp::SHF_ALLOC))
  1576. {
  1577. if (strcmp(name, ".debug_info") == 0
  1578. || strcmp(name, ".zdebug_info") == 0)
  1579. debug_info_sections.push_back(i);
  1580. else if (strcmp(name, ".debug_types") == 0
  1581. || strcmp(name, ".zdebug_types") == 0)
  1582. debug_types_sections.push_back(i);
  1583. }
  1584. }
  1585. }
  1586. if (!is_pass_two)
  1587. layout->layout_gnu_stack(seen_gnu_stack, gnu_stack_flags, this);
  1588. // Handle the .eh_frame sections after the other sections.
  1589. gold_assert(!is_pass_one || eh_frame_sections.empty());
  1590. for (std::vector<unsigned int>::const_iterator p = eh_frame_sections.begin();
  1591. p != eh_frame_sections.end();
  1592. ++p)
  1593. {
  1594. unsigned int i = *p;
  1595. const unsigned char* pshdr;
  1596. pshdr = section_headers_data + i * This::shdr_size;
  1597. typename This::Shdr shdr(pshdr);
  1598. this->layout_eh_frame_section(layout,
  1599. symbols_data,
  1600. symbols_size,
  1601. symbol_names_data,
  1602. symbol_names_size,
  1603. i,
  1604. shdr,
  1605. reloc_shndx[i],
  1606. reloc_type[i]);
  1607. }
  1608. // When doing a relocatable link handle the reloc sections at the
  1609. // end. Garbage collection and Identical Code Folding is not
  1610. // turned on for relocatable code.
  1611. if (emit_relocs)
  1612. this->size_relocatable_relocs();
  1613. gold_assert(!is_two_pass || reloc_sections.empty());
  1614. for (std::vector<unsigned int>::const_iterator p = reloc_sections.begin();
  1615. p != reloc_sections.end();
  1616. ++p)
  1617. {
  1618. unsigned int i = *p;
  1619. const unsigned char* pshdr;
  1620. pshdr = section_headers_data + i * This::shdr_size;
  1621. typename This::Shdr shdr(pshdr);
  1622. unsigned int data_shndx = this->adjust_shndx(shdr.get_sh_info());
  1623. if (data_shndx >= shnum)
  1624. {
  1625. // We already warned about this above.
  1626. continue;
  1627. }
  1628. Output_section* data_section = out_sections[data_shndx];
  1629. if (data_section == reinterpret_cast<Output_section*>(2))
  1630. {
  1631. if (is_pass_two)
  1632. continue;
  1633. // The layout for the data section was deferred, so we need
  1634. // to defer the relocation section, too.
  1635. const char* name = pnames + shdr.get_sh_name();
  1636. this->deferred_layout_relocs_.push_back(
  1637. Deferred_layout(i, name, pshdr, 0, elfcpp::SHT_NULL));
  1638. out_sections[i] = reinterpret_cast<Output_section*>(2);
  1639. out_section_offsets[i] = invalid_address;
  1640. continue;
  1641. }
  1642. if (data_section == NULL)
  1643. {
  1644. out_sections[i] = NULL;
  1645. out_section_offsets[i] = invalid_address;
  1646. continue;
  1647. }
  1648. Relocatable_relocs* rr = new Relocatable_relocs();
  1649. this->set_relocatable_relocs(i, rr);
  1650. Output_section* os = layout->layout_reloc(this, i, shdr, data_section,
  1651. rr);
  1652. out_sections[i] = os;
  1653. out_section_offsets[i] = invalid_address;
  1654. }
  1655. // When building a .gdb_index section, scan the .debug_info and
  1656. // .debug_types sections.
  1657. gold_assert(!is_pass_one
  1658. || (debug_info_sections.empty() && debug_types_sections.empty()));
  1659. for (std::vector<unsigned int>::const_iterator p
  1660. = debug_info_sections.begin();
  1661. p != debug_info_sections.end();
  1662. ++p)
  1663. {
  1664. unsigned int i = *p;
  1665. layout->add_to_gdb_index(false, this, symbols_data, symbols_size,
  1666. i, reloc_shndx[i], reloc_type[i]);
  1667. }
  1668. for (std::vector<unsigned int>::const_iterator p
  1669. = debug_types_sections.begin();
  1670. p != debug_types_sections.end();
  1671. ++p)
  1672. {
  1673. unsigned int i = *p;
  1674. layout->add_to_gdb_index(true, this, symbols_data, symbols_size,
  1675. i, reloc_shndx[i], reloc_type[i]);
  1676. }
  1677. if (is_pass_two)
  1678. {
  1679. delete[] gc_sd->section_headers_data;
  1680. delete[] gc_sd->section_names_data;
  1681. delete[] gc_sd->symbols_data;
  1682. delete[] gc_sd->symbol_names_data;
  1683. this->set_symbols_data(NULL);
  1684. }
  1685. else
  1686. {
  1687. delete sd->section_headers;
  1688. sd->section_headers = NULL;
  1689. delete sd->section_names;
  1690. sd->section_names = NULL;
  1691. }
  1692. }
  1693. // Layout sections whose layout was deferred while waiting for
  1694. // input files from a plugin.
  1695. template<int size, bool big_endian>
  1696. void
  1697. Sized_relobj_file<size, big_endian>::do_layout_deferred_sections(Layout* layout)
  1698. {
  1699. typename std::vector<Deferred_layout>::iterator deferred;
  1700. for (deferred = this->deferred_layout_.begin();
  1701. deferred != this->deferred_layout_.end();
  1702. ++deferred)
  1703. {
  1704. typename This::Shdr shdr(deferred->shdr_data_);
  1705. if (!parameters->options().relocatable()
  1706. && deferred->name_ == ".eh_frame"
  1707. && this->check_eh_frame_flags(&shdr))
  1708. {
  1709. // Checking is_section_included is not reliable for
  1710. // .eh_frame sections, because they do not have an output
  1711. // section. This is not a problem normally because we call
  1712. // layout_eh_frame_section unconditionally, but when
  1713. // deferring sections that is not true. We don't want to
  1714. // keep all .eh_frame sections because that will cause us to
  1715. // keep all sections that they refer to, which is the wrong
  1716. // way around. Instead, the eh_frame code will discard
  1717. // .eh_frame sections that refer to discarded sections.
  1718. // Reading the symbols again here may be slow.
  1719. Read_symbols_data sd;
  1720. this->base_read_symbols(&sd);
  1721. this->layout_eh_frame_section(layout,
  1722. sd.symbols->data(),
  1723. sd.symbols_size,
  1724. sd.symbol_names->data(),
  1725. sd.symbol_names_size,
  1726. deferred->shndx_,
  1727. shdr,
  1728. deferred->reloc_shndx_,
  1729. deferred->reloc_type_);
  1730. continue;
  1731. }
  1732. // If the section is not included, it is because the garbage collector
  1733. // decided it is not needed. Avoid reverting that decision.
  1734. if (!this->is_section_included(deferred->shndx_))
  1735. continue;
  1736. this->layout_section(layout, deferred->shndx_, deferred->name_.c_str(),
  1737. shdr, deferred->reloc_shndx_,
  1738. deferred->reloc_type_);
  1739. }
  1740. this->deferred_layout_.clear();
  1741. // Now handle the deferred relocation sections.
  1742. Output_sections& out_sections(this->output_sections());
  1743. std::vector<Address>& out_section_offsets(this->section_offsets());
  1744. for (deferred = this->deferred_layout_relocs_.begin();
  1745. deferred != this->deferred_layout_relocs_.end();
  1746. ++deferred)
  1747. {
  1748. unsigned int shndx = deferred->shndx_;
  1749. typename This::Shdr shdr(deferred->shdr_data_);
  1750. unsigned int data_shndx = this->adjust_shndx(shdr.get_sh_info());
  1751. Output_section* data_section = out_sections[data_shndx];
  1752. if (data_section == NULL)
  1753. {
  1754. out_sections[shndx] = NULL;
  1755. out_section_offsets[shndx] = invalid_address;
  1756. continue;
  1757. }
  1758. Relocatable_relocs* rr = new Relocatable_relocs();
  1759. this->set_relocatable_relocs(shndx, rr);
  1760. Output_section* os = layout->layout_reloc(this, shndx, shdr,
  1761. data_section, rr);
  1762. out_sections[shndx] = os;
  1763. out_section_offsets[shndx] = invalid_address;
  1764. }
  1765. }
  1766. // Add the symbols to the symbol table.
  1767. template<int size, bool big_endian>
  1768. void
  1769. Sized_relobj_file<size, big_endian>::do_add_symbols(Symbol_table* symtab,
  1770. Read_symbols_data* sd,
  1771. Layout*)
  1772. {
  1773. if (sd->symbols == NULL)
  1774. {
  1775. gold_assert(sd->symbol_names == NULL);
  1776. return;
  1777. }
  1778. const int sym_size = This::sym_size;
  1779. size_t symcount = ((sd->symbols_size - sd->external_symbols_offset)
  1780. / sym_size);
  1781. if (symcount * sym_size != sd->symbols_size - sd->external_symbols_offset)
  1782. {
  1783. this->error(_("size of symbols is not multiple of symbol size"));
  1784. return;
  1785. }
  1786. this->symbols_.resize(symcount);
  1787. const char* sym_names =
  1788. reinterpret_cast<const char*>(sd->symbol_names->data());
  1789. symtab->add_from_relobj(this,
  1790. sd->symbols->data() + sd->external_symbols_offset,
  1791. symcount, this->local_symbol_count_,
  1792. sym_names, sd->symbol_names_size,
  1793. &this->symbols_,
  1794. &this->defined_count_);
  1795. delete sd->symbols;
  1796. sd->symbols = NULL;
  1797. delete sd->symbol_names;
  1798. sd->symbol_names = NULL;
  1799. }
  1800. // Find out if this object, that is a member of a lib group, should be included
  1801. // in the link. We check every symbol defined by this object. If the symbol
  1802. // table has a strong undefined reference to that symbol, we have to include
  1803. // the object.
  1804. template<int size, bool big_endian>
  1805. Archive::Should_include
  1806. Sized_relobj_file<size, big_endian>::do_should_include_member(
  1807. Symbol_table* symtab,
  1808. Layout* layout,
  1809. Read_symbols_data* sd,
  1810. std::string* why)
  1811. {
  1812. char* tmpbuf = NULL;
  1813. size_t tmpbuflen = 0;
  1814. const char* sym_names =
  1815. reinterpret_cast<const char*>(sd->symbol_names->data());
  1816. const unsigned char* syms =
  1817. sd->symbols->data() + sd->external_symbols_offset;
  1818. const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
  1819. size_t symcount = ((sd->symbols_size - sd->external_symbols_offset)
  1820. / sym_size);
  1821. const unsigned char* p = syms;
  1822. for (size_t i = 0; i < symcount; ++i, p += sym_size)
  1823. {
  1824. elfcpp::Sym<size, big_endian> sym(p);
  1825. unsigned int st_shndx = sym.get_st_shndx();
  1826. if (st_shndx == elfcpp::SHN_UNDEF)
  1827. continue;
  1828. unsigned int st_name = sym.get_st_name();
  1829. const char* name = sym_names + st_name;
  1830. Symbol* symbol;
  1831. Archive::Should_include t = Archive::should_include_member(symtab,
  1832. layout,
  1833. name,
  1834. &symbol, why,
  1835. &tmpbuf,
  1836. &tmpbuflen);
  1837. if (t == Archive::SHOULD_INCLUDE_YES)
  1838. {
  1839. if (tmpbuf != NULL)
  1840. free(tmpbuf);
  1841. return t;
  1842. }
  1843. }
  1844. if (tmpbuf != NULL)
  1845. free(tmpbuf);
  1846. return Archive::SHOULD_INCLUDE_UNKNOWN;
  1847. }
  1848. // Iterate over global defined symbols, calling a visitor class V for each.
  1849. template<int size, bool big_endian>
  1850. void
  1851. Sized_relobj_file<size, big_endian>::do_for_all_global_symbols(
  1852. Read_symbols_data* sd,
  1853. Library_base::Symbol_visitor_base* v)
  1854. {
  1855. const char* sym_names =
  1856. reinterpret_cast<const char*>(sd->symbol_names->data());
  1857. const unsigned char* syms =
  1858. sd->symbols->data() + sd->external_symbols_offset;
  1859. const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
  1860. size_t symcount = ((sd->symbols_size - sd->external_symbols_offset)
  1861. / sym_size);
  1862. const unsigned char* p = syms;
  1863. for (size_t i = 0; i < symcount; ++i, p += sym_size)
  1864. {
  1865. elfcpp::Sym<size, big_endian> sym(p);
  1866. if (sym.get_st_shndx() != elfcpp::SHN_UNDEF)
  1867. v->visit(sym_names + sym.get_st_name());
  1868. }
  1869. }
  1870. // Return whether the local symbol SYMNDX has a PLT offset.
  1871. template<int size, bool big_endian>
  1872. bool
  1873. Sized_relobj_file<size, big_endian>::local_has_plt_offset(
  1874. unsigned int symndx) const
  1875. {
  1876. typename Local_plt_offsets::const_iterator p =
  1877. this->local_plt_offsets_.find(symndx);
  1878. return p != this->local_plt_offsets_.end();
  1879. }
  1880. // Get the PLT offset of a local symbol.
  1881. template<int size, bool big_endian>
  1882. unsigned int
  1883. Sized_relobj_file<size, big_endian>::do_local_plt_offset(
  1884. unsigned int symndx) const
  1885. {
  1886. typename Local_plt_offsets::const_iterator p =
  1887. this->local_plt_offsets_.find(symndx);
  1888. gold_assert(p != this->local_plt_offsets_.end());
  1889. return p->second;
  1890. }
  1891. // Set the PLT offset of a local symbol.
  1892. template<int size, bool big_endian>
  1893. void
  1894. Sized_relobj_file<size, big_endian>::set_local_plt_offset(
  1895. unsigned int symndx, unsigned int plt_offset)
  1896. {
  1897. std::pair<typename Local_plt_offsets::iterator, bool> ins =
  1898. this->local_plt_offsets_.insert(std::make_pair(symndx, plt_offset));
  1899. gold_assert(ins.second);
  1900. }
  1901. // First pass over the local symbols. Here we add their names to
  1902. // *POOL and *DYNPOOL, and we store the symbol value in
  1903. // THIS->LOCAL_VALUES_. This function is always called from a
  1904. // singleton thread. This is followed by a call to
  1905. // finalize_local_symbols.
  1906. template<int size, bool big_endian>
  1907. void
  1908. Sized_relobj_file<size, big_endian>::do_count_local_symbols(Stringpool* pool,
  1909. Stringpool* dynpool)
  1910. {
  1911. gold_assert(this->symtab_shndx_ != -1U);
  1912. if (this->symtab_shndx_ == 0)
  1913. {
  1914. // This object has no symbols. Weird but legal.
  1915. return;
  1916. }
  1917. // Read the symbol table section header.
  1918. const unsigned int symtab_shndx = this->symtab_shndx_;
  1919. typename This::Shdr symtabshdr(this,
  1920. this->elf_file_.section_header(symtab_shndx));
  1921. gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
  1922. // Read the local symbols.
  1923. const int sym_size = This::sym_size;
  1924. const unsigned int loccount = this->local_symbol_count_;
  1925. gold_assert(loccount == symtabshdr.get_sh_info());
  1926. off_t locsize = loccount * sym_size;
  1927. const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
  1928. locsize, true, true);
  1929. // Read the symbol names.
  1930. const unsigned int strtab_shndx =
  1931. this->adjust_shndx(symtabshdr.get_sh_link());
  1932. section_size_type strtab_size;
  1933. const unsigned char* pnamesu = this->section_contents(strtab_shndx,
  1934. &strtab_size,
  1935. true);
  1936. const char* pnames = reinterpret_cast<const char*>(pnamesu);
  1937. // Loop over the local symbols.
  1938. const Output_sections& out_sections(this->output_sections());
  1939. std::vector<Address>& out_section_offsets(this->section_offsets());
  1940. unsigned int shnum = this->shnum();
  1941. unsigned int count = 0;
  1942. unsigned int dyncount = 0;
  1943. // Skip the first, dummy, symbol.
  1944. psyms += sym_size;
  1945. bool strip_all = parameters->options().strip_all();
  1946. bool discard_all = parameters->options().discard_all();
  1947. bool discard_locals = parameters->options().discard_locals();
  1948. bool discard_sec_merge = parameters->options().discard_sec_merge();
  1949. for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
  1950. {
  1951. elfcpp::Sym<size, big_endian> sym(psyms);
  1952. Symbol_value<size>& lv(this->local_values_[i]);
  1953. bool is_ordinary;
  1954. unsigned int shndx = this->adjust_sym_shndx(i, sym.get_st_shndx(),
  1955. &is_ordinary);
  1956. lv.set_input_shndx(shndx, is_ordinary);
  1957. if (sym.get_st_type() == elfcpp::STT_SECTION)
  1958. lv.set_is_section_symbol();
  1959. else if (sym.get_st_type() == elfcpp::STT_TLS)
  1960. lv.set_is_tls_symbol();
  1961. else if (sym.get_st_type() == elfcpp::STT_GNU_IFUNC)
  1962. lv.set_is_ifunc_symbol();
  1963. // Save the input symbol value for use in do_finalize_local_symbols().
  1964. lv.set_input_value(sym.get_st_value());
  1965. // Decide whether this symbol should go into the output file.
  1966. if ((shndx < shnum && out_sections[shndx] == NULL)
  1967. || shndx == this->discarded_eh_frame_shndx_)
  1968. {
  1969. lv.set_no_output_symtab_entry();
  1970. gold_assert(!lv.needs_output_dynsym_entry());
  1971. continue;
  1972. }
  1973. if (sym.get_st_type() == elfcpp::STT_SECTION
  1974. || !this->adjust_local_symbol(&lv))
  1975. {
  1976. lv.set_no_output_symtab_entry();
  1977. gold_assert(!lv.needs_output_dynsym_entry());
  1978. continue;
  1979. }
  1980. if (sym.get_st_name() >= strtab_size)
  1981. {
  1982. this->error(_("local symbol %u section name out of range: %u >= %u"),
  1983. i, sym.get_st_name(),
  1984. static_cast<unsigned int>(strtab_size));
  1985. lv.set_no_output_symtab_entry();
  1986. continue;
  1987. }
  1988. const char* name = pnames + sym.get_st_name();
  1989. // If needed, add the symbol to the dynamic symbol table string pool.
  1990. if (lv.needs_output_dynsym_entry())
  1991. {
  1992. dynpool->add(name, true, NULL);
  1993. ++dyncount;
  1994. }
  1995. if (strip_all
  1996. || (discard_all && lv.may_be_discarded_from_output_symtab()))
  1997. {
  1998. lv.set_no_output_symtab_entry();
  1999. continue;
  2000. }
  2001. // By default, discard temporary local symbols in merge sections.
  2002. // If --discard-locals option is used, discard all temporary local
  2003. // symbols. These symbols start with system-specific local label
  2004. // prefixes, typically .L for ELF system. We want to be compatible
  2005. // with GNU ld so here we essentially use the same check in
  2006. // bfd_is_local_label(). The code is different because we already
  2007. // know that:
  2008. //
  2009. // - the symbol is local and thus cannot have global or weak binding.
  2010. // - the symbol is not a section symbol.
  2011. // - the symbol has a name.
  2012. //
  2013. // We do not discard a symbol if it needs a dynamic symbol entry.
  2014. if ((discard_locals
  2015. || (discard_sec_merge
  2016. && is_ordinary
  2017. && out_section_offsets[shndx] == invalid_address))
  2018. && sym.get_st_type() != elfcpp::STT_FILE
  2019. && !lv.needs_output_dynsym_entry()
  2020. && lv.may_be_discarded_from_output_symtab()
  2021. && parameters->target().is_local_label_name(name))
  2022. {
  2023. lv.set_no_output_symtab_entry();
  2024. continue;
  2025. }
  2026. // Discard the local symbol if -retain_symbols_file is specified
  2027. // and the local symbol is not in that file.
  2028. if (!parameters->options().should_retain_symbol(name))
  2029. {
  2030. lv.set_no_output_symtab_entry();
  2031. continue;
  2032. }
  2033. // Add the symbol to the symbol table string pool.
  2034. pool->add(name, true, NULL);
  2035. ++count;
  2036. }
  2037. this->output_local_symbol_count_ = count;
  2038. this->output_local_dynsym_count_ = dyncount;
  2039. }
  2040. // Compute the final value of a local symbol.
  2041. template<int size, bool big_endian>
  2042. typename Sized_relobj_file<size, big_endian>::Compute_final_local_value_status
  2043. Sized_relobj_file<size, big_endian>::compute_final_local_value_internal(
  2044. unsigned int r_sym,
  2045. const Symbol_value<size>* lv_in,
  2046. Symbol_value<size>* lv_out,
  2047. bool relocatable,
  2048. const Output_sections& out_sections,
  2049. const std::vector<Address>& out_offsets,
  2050. const Symbol_table* symtab)
  2051. {
  2052. // We are going to overwrite *LV_OUT, if it has a merged symbol value,
  2053. // we may have a memory leak.
  2054. gold_assert(lv_out->has_output_value());
  2055. bool is_ordinary;
  2056. unsigned int shndx = lv_in->input_shndx(&is_ordinary);
  2057. // Set the output symbol value.
  2058. if (!is_ordinary)
  2059. {
  2060. if (shndx == elfcpp::SHN_ABS || Symbol::is_common_shndx(shndx))
  2061. lv_out->set_output_value(lv_in->input_value());
  2062. else
  2063. {
  2064. this->error(_("unknown section index %u for local symbol %u"),
  2065. shndx, r_sym);
  2066. lv_out->set_output_value(0);
  2067. return This::CFLV_ERROR;
  2068. }
  2069. }
  2070. else
  2071. {
  2072. if (shndx >= this->shnum())
  2073. {
  2074. this->error(_("local symbol %u section index %u out of range"),
  2075. r_sym, shndx);
  2076. lv_out->set_output_value(0);
  2077. return This::CFLV_ERROR;
  2078. }
  2079. Output_section* os = out_sections[shndx];
  2080. Address secoffset = out_offsets[shndx];
  2081. if (symtab->is_section_folded(this, shndx))
  2082. {
  2083. gold_assert(os == NULL && secoffset == invalid_address);
  2084. // Get the os of the section it is folded onto.
  2085. Section_id folded = symtab->icf()->get_folded_section(this,
  2086. shndx);
  2087. gold_assert(folded.first != NULL);
  2088. Sized_relobj_file<size, big_endian>* folded_obj = reinterpret_cast
  2089. <Sized_relobj_file<size, big_endian>*>(folded.first);
  2090. os = folded_obj->output_section(folded.second);
  2091. gold_assert(os != NULL);
  2092. secoffset = folded_obj->get_output_section_offset(folded.second);
  2093. // This could be a relaxed input section.
  2094. if (secoffset == invalid_address)
  2095. {
  2096. const Output_relaxed_input_section* relaxed_section =
  2097. os->find_relaxed_input_section(folded_obj, folded.second);
  2098. gold_assert(relaxed_section != NULL);
  2099. secoffset = relaxed_section->address() - os->address();
  2100. }
  2101. }
  2102. if (os == NULL)
  2103. {
  2104. // This local symbol belongs to a section we are discarding.
  2105. // In some cases when applying relocations later, we will
  2106. // attempt to match it to the corresponding kept section,
  2107. // so we leave the input value unchanged here.
  2108. return This::CFLV_DISCARDED;
  2109. }
  2110. else if (secoffset == invalid_address)
  2111. {
  2112. uint64_t start;
  2113. // This is a SHF_MERGE section or one which otherwise
  2114. // requires special handling.
  2115. if (shndx == this->discarded_eh_frame_shndx_)
  2116. {
  2117. // This local symbol belongs to a discarded .eh_frame
  2118. // section. Just treat it like the case in which
  2119. // os == NULL above.
  2120. gold_assert(this->has_eh_frame_);
  2121. return This::CFLV_DISCARDED;
  2122. }
  2123. else if (!lv_in->is_section_symbol())
  2124. {
  2125. // This is not a section symbol. We can determine
  2126. // the final value now.
  2127. lv_out->set_output_value(
  2128. os->output_address(this, shndx, lv_in->input_value()));
  2129. }
  2130. else if (!os->find_starting_output_address(this, shndx, &start))
  2131. {
  2132. // This is a section symbol, but apparently not one in a
  2133. // merged section. First check to see if this is a relaxed
  2134. // input section. If so, use its address. Otherwise just
  2135. // use the start of the output section. This happens with
  2136. // relocatable links when the input object has section
  2137. // symbols for arbitrary non-merge sections.
  2138. const Output_section_data* posd =
  2139. os->find_relaxed_input_section(this, shndx);
  2140. if (posd != NULL)
  2141. {
  2142. Address relocatable_link_adjustment =
  2143. relocatable ? os->address() : 0;
  2144. lv_out->set_output_value(posd->address()
  2145. - relocatable_link_adjustment);
  2146. }
  2147. else
  2148. lv_out->set_output_value(os->address());
  2149. }
  2150. else
  2151. {
  2152. // We have to consider the addend to determine the
  2153. // value to use in a relocation. START is the start
  2154. // of this input section. If we are doing a relocatable
  2155. // link, use offset from start output section instead of
  2156. // address.
  2157. Address adjusted_start =
  2158. relocatable ? start - os->address() : start;
  2159. Merged_symbol_value<size>* msv =
  2160. new Merged_symbol_value<size>(lv_in->input_value(),
  2161. adjusted_start);
  2162. lv_out->set_merged_symbol_value(msv);
  2163. }
  2164. }
  2165. else if (lv_in->is_tls_symbol()
  2166. || (lv_in->is_section_symbol()
  2167. && (os->flags() & elfcpp::SHF_TLS)))
  2168. lv_out->set_output_value(os->tls_offset()
  2169. + secoffset
  2170. + lv_in->input_value());
  2171. else
  2172. lv_out->set_output_value((relocatable ? 0 : os->address())
  2173. + secoffset
  2174. + lv_in->input_value());
  2175. }
  2176. return This::CFLV_OK;
  2177. }
  2178. // Compute final local symbol value. R_SYM is the index of a local
  2179. // symbol in symbol table. LV points to a symbol value, which is
  2180. // expected to hold the input value and to be over-written by the
  2181. // final value. SYMTAB points to a symbol table. Some targets may want
  2182. // to know would-be-finalized local symbol values in relaxation.
  2183. // Hence we provide this method. Since this method updates *LV, a
  2184. // callee should make a copy of the original local symbol value and
  2185. // use the copy instead of modifying an object's local symbols before
  2186. // everything is finalized. The caller should also free up any allocated
  2187. // memory in the return value in *LV.
  2188. template<int size, bool big_endian>
  2189. typename Sized_relobj_file<size, big_endian>::Compute_final_local_value_status
  2190. Sized_relobj_file<size, big_endian>::compute_final_local_value(
  2191. unsigned int r_sym,
  2192. const Symbol_value<size>* lv_in,
  2193. Symbol_value<size>* lv_out,
  2194. const Symbol_table* symtab)
  2195. {
  2196. // This is just a wrapper of compute_final_local_value_internal.
  2197. const bool relocatable = parameters->options().relocatable();
  2198. const Output_sections& out_sections(this->output_sections());
  2199. const std::vector<Address>& out_offsets(this->section_offsets());
  2200. return this->compute_final_local_value_internal(r_sym, lv_in, lv_out,
  2201. relocatable, out_sections,
  2202. out_offsets, symtab);
  2203. }
  2204. // Finalize the local symbols. Here we set the final value in
  2205. // THIS->LOCAL_VALUES_ and set their output symbol table indexes.
  2206. // This function is always called from a singleton thread. The actual
  2207. // output of the local symbols will occur in a separate task.
  2208. template<int size, bool big_endian>
  2209. unsigned int
  2210. Sized_relobj_file<size, big_endian>::do_finalize_local_symbols(
  2211. unsigned int index,
  2212. off_t off,
  2213. Symbol_table* symtab)
  2214. {
  2215. gold_assert(off == static_cast<off_t>(align_address(off, size >> 3)));
  2216. const unsigned int loccount = this->local_symbol_count_;
  2217. this->local_symbol_offset_ = off;
  2218. const bool relocatable = parameters->options().relocatable();
  2219. const Output_sections& out_sections(this->output_sections());
  2220. const std::vector<Address>& out_offsets(this->section_offsets());
  2221. for (unsigned int i = 1; i < loccount; ++i)
  2222. {
  2223. Symbol_value<size>* lv = &this->local_values_[i];
  2224. Compute_final_local_value_status cflv_status =
  2225. this->compute_final_local_value_internal(i, lv, lv, relocatable,
  2226. out_sections, out_offsets,
  2227. symtab);
  2228. switch (cflv_status)
  2229. {
  2230. case CFLV_OK:
  2231. if (!lv->is_output_symtab_index_set())
  2232. {
  2233. lv->set_output_symtab_index(index);
  2234. ++index;
  2235. }
  2236. break;
  2237. case CFLV_DISCARDED:
  2238. case CFLV_ERROR:
  2239. // Do nothing.
  2240. break;
  2241. default:
  2242. gold_unreachable();
  2243. }
  2244. }
  2245. return index;
  2246. }
  2247. // Set the output dynamic symbol table indexes for the local variables.
  2248. template<int size, bool big_endian>
  2249. unsigned int
  2250. Sized_relobj_file<size, big_endian>::do_set_local_dynsym_indexes(
  2251. unsigned int index)
  2252. {
  2253. const unsigned int loccount = this->local_symbol_count_;
  2254. for (unsigned int i = 1; i < loccount; ++i)
  2255. {
  2256. Symbol_value<size>& lv(this->local_values_[i]);
  2257. if (lv.needs_output_dynsym_entry())
  2258. {
  2259. lv.set_output_dynsym_index(index);
  2260. ++index;
  2261. }
  2262. }
  2263. return index;
  2264. }
  2265. // Set the offset where local dynamic symbol information will be stored.
  2266. // Returns the count of local symbols contributed to the symbol table by
  2267. // this object.
  2268. template<int size, bool big_endian>
  2269. unsigned int
  2270. Sized_relobj_file<size, big_endian>::do_set_local_dynsym_offset(off_t off)
  2271. {
  2272. gold_assert(off == static_cast<off_t>(align_address(off, size >> 3)));
  2273. this->local_dynsym_offset_ = off;
  2274. return this->output_local_dynsym_count_;
  2275. }
  2276. // If Symbols_data is not NULL get the section flags from here otherwise
  2277. // get it from the file.
  2278. template<int size, bool big_endian>
  2279. uint64_t
  2280. Sized_relobj_file<size, big_endian>::do_section_flags(unsigned int shndx)
  2281. {
  2282. Symbols_data* sd = this->get_symbols_data();
  2283. if (sd != NULL)
  2284. {
  2285. const unsigned char* pshdrs = sd->section_headers_data
  2286. + This::shdr_size * shndx;
  2287. typename This::Shdr shdr(pshdrs);
  2288. return shdr.get_sh_flags();
  2289. }
  2290. // If sd is NULL, read the section header from the file.
  2291. return this->elf_file_.section_flags(shndx);
  2292. }
  2293. // Get the section's ent size from Symbols_data. Called by get_section_contents
  2294. // in icf.cc
  2295. template<int size, bool big_endian>
  2296. uint64_t
  2297. Sized_relobj_file<size, big_endian>::do_section_entsize(unsigned int shndx)
  2298. {
  2299. Symbols_data* sd = this->get_symbols_data();
  2300. gold_assert(sd != NULL);
  2301. const unsigned char* pshdrs = sd->section_headers_data
  2302. + This::shdr_size * shndx;
  2303. typename This::Shdr shdr(pshdrs);
  2304. return shdr.get_sh_entsize();
  2305. }
  2306. // Write out the local symbols.
  2307. template<int size, bool big_endian>
  2308. void
  2309. Sized_relobj_file<size, big_endian>::write_local_symbols(
  2310. Output_file* of,
  2311. const Stringpool* sympool,
  2312. const Stringpool* dynpool,
  2313. Output_symtab_xindex* symtab_xindex,
  2314. Output_symtab_xindex* dynsym_xindex,
  2315. off_t symtab_off)
  2316. {
  2317. const bool strip_all = parameters->options().strip_all();
  2318. if (strip_all)
  2319. {
  2320. if (this->output_local_dynsym_count_ == 0)
  2321. return;
  2322. this->output_local_symbol_count_ = 0;
  2323. }
  2324. gold_assert(this->symtab_shndx_ != -1U);
  2325. if (this->symtab_shndx_ == 0)
  2326. {
  2327. // This object has no symbols. Weird but legal.
  2328. return;
  2329. }
  2330. // Read the symbol table section header.
  2331. const unsigned int symtab_shndx = this->symtab_shndx_;
  2332. typename This::Shdr symtabshdr(this,
  2333. this->elf_file_.section_header(symtab_shndx));
  2334. gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
  2335. const unsigned int loccount = this->local_symbol_count_;
  2336. gold_assert(loccount == symtabshdr.get_sh_info());
  2337. // Read the local symbols.
  2338. const int sym_size = This::sym_size;
  2339. off_t locsize = loccount * sym_size;
  2340. const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
  2341. locsize, true, false);
  2342. // Read the symbol names.
  2343. const unsigned int strtab_shndx =
  2344. this->adjust_shndx(symtabshdr.get_sh_link());
  2345. section_size_type strtab_size;
  2346. const unsigned char* pnamesu = this->section_contents(strtab_shndx,
  2347. &strtab_size,
  2348. false);
  2349. const char* pnames = reinterpret_cast<const char*>(pnamesu);
  2350. // Get views into the output file for the portions of the symbol table
  2351. // and the dynamic symbol table that we will be writing.
  2352. off_t output_size = this->output_local_symbol_count_ * sym_size;
  2353. unsigned char* oview = NULL;
  2354. if (output_size > 0)
  2355. oview = of->get_output_view(symtab_off + this->local_symbol_offset_,
  2356. output_size);
  2357. off_t dyn_output_size = this->output_local_dynsym_count_ * sym_size;
  2358. unsigned char* dyn_oview = NULL;
  2359. if (dyn_output_size > 0)
  2360. dyn_oview = of->get_output_view(this->local_dynsym_offset_,
  2361. dyn_output_size);
  2362. const Output_sections& out_sections(this->output_sections());
  2363. gold_assert(this->local_values_.size() == loccount);
  2364. unsigned char* ov = oview;
  2365. unsigned char* dyn_ov = dyn_oview;
  2366. psyms += sym_size;
  2367. for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
  2368. {
  2369. elfcpp::Sym<size, big_endian> isym(psyms);
  2370. Symbol_value<size>& lv(this->local_values_[i]);
  2371. bool is_ordinary;
  2372. unsigned int st_shndx = this->adjust_sym_shndx(i, isym.get_st_shndx(),
  2373. &is_ordinary);
  2374. if (is_ordinary)
  2375. {
  2376. gold_assert(st_shndx < out_sections.size());
  2377. if (out_sections[st_shndx] == NULL)
  2378. continue;
  2379. st_shndx = out_sections[st_shndx]->out_shndx();
  2380. if (st_shndx >= elfcpp::SHN_LORESERVE)
  2381. {
  2382. if (lv.has_output_symtab_entry())
  2383. symtab_xindex->add(lv.output_symtab_index(), st_shndx);
  2384. if (lv.has_output_dynsym_entry())
  2385. dynsym_xindex->add(lv.output_dynsym_index(), st_shndx);
  2386. st_shndx = elfcpp::SHN_XINDEX;
  2387. }
  2388. }
  2389. // Write the symbol to the output symbol table.
  2390. if (lv.has_output_symtab_entry())
  2391. {
  2392. elfcpp::Sym_write<size, big_endian> osym(ov);
  2393. gold_assert(isym.get_st_name() < strtab_size);
  2394. const char* name = pnames + isym.get_st_name();
  2395. osym.put_st_name(sympool->get_offset(name));
  2396. osym.put_st_value(this->local_values_[i].value(this, 0));
  2397. osym.put_st_size(isym.get_st_size());
  2398. osym.put_st_info(isym.get_st_info());
  2399. osym.put_st_other(isym.get_st_other());
  2400. osym.put_st_shndx(st_shndx);
  2401. ov += sym_size;
  2402. }
  2403. // Write the symbol to the output dynamic symbol table.
  2404. if (lv.has_output_dynsym_entry())
  2405. {
  2406. gold_assert(dyn_ov < dyn_oview + dyn_output_size);
  2407. elfcpp::Sym_write<size, big_endian> osym(dyn_ov);
  2408. gold_assert(isym.get_st_name() < strtab_size);
  2409. const char* name = pnames + isym.get_st_name();
  2410. osym.put_st_name(dynpool->get_offset(name));
  2411. osym.put_st_value(this->local_values_[i].value(this, 0));
  2412. osym.put_st_size(isym.get_st_size());
  2413. osym.put_st_info(isym.get_st_info());
  2414. osym.put_st_other(isym.get_st_other());
  2415. osym.put_st_shndx(st_shndx);
  2416. dyn_ov += sym_size;
  2417. }
  2418. }
  2419. if (output_size > 0)
  2420. {
  2421. gold_assert(ov - oview == output_size);
  2422. of->write_output_view(symtab_off + this->local_symbol_offset_,
  2423. output_size, oview);
  2424. }
  2425. if (dyn_output_size > 0)
  2426. {
  2427. gold_assert(dyn_ov - dyn_oview == dyn_output_size);
  2428. of->write_output_view(this->local_dynsym_offset_, dyn_output_size,
  2429. dyn_oview);
  2430. }
  2431. }
  2432. // Set *INFO to symbolic information about the offset OFFSET in the
  2433. // section SHNDX. Return true if we found something, false if we
  2434. // found nothing.
  2435. template<int size, bool big_endian>
  2436. bool
  2437. Sized_relobj_file<size, big_endian>::get_symbol_location_info(
  2438. unsigned int shndx,
  2439. off_t offset,
  2440. Symbol_location_info* info)
  2441. {
  2442. if (this->symtab_shndx_ == 0)
  2443. return false;
  2444. section_size_type symbols_size;
  2445. const unsigned char* symbols = this->section_contents(this->symtab_shndx_,
  2446. &symbols_size,
  2447. false);
  2448. unsigned int symbol_names_shndx =
  2449. this->adjust_shndx(this->section_link(this->symtab_shndx_));
  2450. section_size_type names_size;
  2451. const unsigned char* symbol_names_u =
  2452. this->section_contents(symbol_names_shndx, &names_size, false);
  2453. const char* symbol_names = reinterpret_cast<const char*>(symbol_names_u);
  2454. const int sym_size = This::sym_size;
  2455. const size_t count = symbols_size / sym_size;
  2456. const unsigned char* p = symbols;
  2457. for (size_t i = 0; i < count; ++i, p += sym_size)
  2458. {
  2459. elfcpp::Sym<size, big_endian> sym(p);
  2460. if (sym.get_st_type() == elfcpp::STT_FILE)
  2461. {
  2462. if (sym.get_st_name() >= names_size)
  2463. info->source_file = "(invalid)";
  2464. else
  2465. info->source_file = symbol_names + sym.get_st_name();
  2466. continue;
  2467. }
  2468. bool is_ordinary;
  2469. unsigned int st_shndx = this->adjust_sym_shndx(i, sym.get_st_shndx(),
  2470. &is_ordinary);
  2471. if (is_ordinary
  2472. && st_shndx == shndx
  2473. && static_cast<off_t>(sym.get_st_value()) <= offset
  2474. && (static_cast<off_t>(sym.get_st_value() + sym.get_st_size())
  2475. > offset))
  2476. {
  2477. info->enclosing_symbol_type = sym.get_st_type();
  2478. if (sym.get_st_name() > names_size)
  2479. info->enclosing_symbol_name = "(invalid)";
  2480. else
  2481. {
  2482. info->enclosing_symbol_name = symbol_names + sym.get_st_name();
  2483. if (parameters->options().do_demangle())
  2484. {
  2485. char* demangled_name = cplus_demangle(
  2486. info->enclosing_symbol_name.c_str(),
  2487. DMGL_ANSI | DMGL_PARAMS);
  2488. if (demangled_name != NULL)
  2489. {
  2490. info->enclosing_symbol_name.assign(demangled_name);
  2491. free(demangled_name);
  2492. }
  2493. }
  2494. }
  2495. return true;
  2496. }
  2497. }
  2498. return false;
  2499. }
  2500. // Look for a kept section corresponding to the given discarded section,
  2501. // and return its output address. This is used only for relocations in
  2502. // debugging sections. If we can't find the kept section, return 0.
  2503. template<int size, bool big_endian>
  2504. typename Sized_relobj_file<size, big_endian>::Address
  2505. Sized_relobj_file<size, big_endian>::map_to_kept_section(
  2506. unsigned int shndx,
  2507. bool* found) const
  2508. {
  2509. Relobj* kept_object;
  2510. unsigned int kept_shndx;
  2511. if (this->get_kept_comdat_section(shndx, &kept_object, &kept_shndx))
  2512. {
  2513. Sized_relobj_file<size, big_endian>* kept_relobj =
  2514. static_cast<Sized_relobj_file<size, big_endian>*>(kept_object);
  2515. Output_section* os = kept_relobj->output_section(kept_shndx);
  2516. Address offset = kept_relobj->get_output_section_offset(kept_shndx);
  2517. if (os != NULL && offset != invalid_address)
  2518. {
  2519. *found = true;
  2520. return os->address() + offset;
  2521. }
  2522. }
  2523. *found = false;
  2524. return 0;
  2525. }
  2526. // Get symbol counts.
  2527. template<int size, bool big_endian>
  2528. void
  2529. Sized_relobj_file<size, big_endian>::do_get_global_symbol_counts(
  2530. const Symbol_table*,
  2531. size_t* defined,
  2532. size_t* used) const
  2533. {
  2534. *defined = this->defined_count_;
  2535. size_t count = 0;
  2536. for (typename Symbols::const_iterator p = this->symbols_.begin();
  2537. p != this->symbols_.end();
  2538. ++p)
  2539. if (*p != NULL
  2540. && (*p)->source() == Symbol::FROM_OBJECT
  2541. && (*p)->object() == this
  2542. && (*p)->is_defined())
  2543. ++count;
  2544. *used = count;
  2545. }
  2546. // Return a view of the decompressed contents of a section. Set *PLEN
  2547. // to the size. Set *IS_NEW to true if the contents need to be freed
  2548. // by the caller.
  2549. const unsigned char*
  2550. Object::decompressed_section_contents(
  2551. unsigned int shndx,
  2552. section_size_type* plen,
  2553. bool* is_new)
  2554. {
  2555. section_size_type buffer_size;
  2556. const unsigned char* buffer = this->do_section_contents(shndx, &buffer_size,
  2557. false);
  2558. if (this->compressed_sections_ == NULL)
  2559. {
  2560. *plen = buffer_size;
  2561. *is_new = false;
  2562. return buffer;
  2563. }
  2564. Compressed_section_map::const_iterator p =
  2565. this->compressed_sections_->find(shndx);
  2566. if (p == this->compressed_sections_->end())
  2567. {
  2568. *plen = buffer_size;
  2569. *is_new = false;
  2570. return buffer;
  2571. }
  2572. section_size_type uncompressed_size = p->second.size;
  2573. if (p->second.contents != NULL)
  2574. {
  2575. *plen = uncompressed_size;
  2576. *is_new = false;
  2577. return p->second.contents;
  2578. }
  2579. unsigned char* uncompressed_data = new unsigned char[uncompressed_size];
  2580. if (!decompress_input_section(buffer,
  2581. buffer_size,
  2582. uncompressed_data,
  2583. uncompressed_size,
  2584. elfsize(),
  2585. is_big_endian(),
  2586. p->second.flag))
  2587. this->error(_("could not decompress section %s"),
  2588. this->do_section_name(shndx).c_str());
  2589. // We could cache the results in p->second.contents and store
  2590. // false in *IS_NEW, but build_compressed_section_map() would
  2591. // have done so if it had expected it to be profitable. If
  2592. // we reach this point, we expect to need the contents only
  2593. // once in this pass.
  2594. *plen = uncompressed_size;
  2595. *is_new = true;
  2596. return uncompressed_data;
  2597. }
  2598. // Discard any buffers of uncompressed sections. This is done
  2599. // at the end of the Add_symbols task.
  2600. void
  2601. Object::discard_decompressed_sections()
  2602. {
  2603. if (this->compressed_sections_ == NULL)
  2604. return;
  2605. for (Compressed_section_map::iterator p = this->compressed_sections_->begin();
  2606. p != this->compressed_sections_->end();
  2607. ++p)
  2608. {
  2609. if (p->second.contents != NULL)
  2610. {
  2611. delete[] p->second.contents;
  2612. p->second.contents = NULL;
  2613. }
  2614. }
  2615. }
  2616. // Input_objects methods.
  2617. // Add a regular relocatable object to the list. Return false if this
  2618. // object should be ignored.
  2619. bool
  2620. Input_objects::add_object(Object* obj)
  2621. {
  2622. // Print the filename if the -t/--trace option is selected.
  2623. if (parameters->options().trace())
  2624. gold_info("%s", obj->name().c_str());
  2625. if (!obj->is_dynamic())
  2626. this->relobj_list_.push_back(static_cast<Relobj*>(obj));
  2627. else
  2628. {
  2629. // See if this is a duplicate SONAME.
  2630. Dynobj* dynobj = static_cast<Dynobj*>(obj);
  2631. const char* soname = dynobj->soname();
  2632. Unordered_map<std::string, Object*>::value_type val(soname, obj);
  2633. std::pair<Unordered_map<std::string, Object*>::iterator, bool> ins =
  2634. this->sonames_.insert(val);
  2635. if (!ins.second)
  2636. {
  2637. // We have already seen a dynamic object with this soname.
  2638. // If any instances of this object on the command line have
  2639. // the --no-as-needed flag, make sure the one we keep is
  2640. // marked so.
  2641. if (!obj->as_needed())
  2642. {
  2643. gold_assert(ins.first->second != NULL);
  2644. ins.first->second->clear_as_needed();
  2645. }
  2646. return false;
  2647. }
  2648. this->dynobj_list_.push_back(dynobj);
  2649. }
  2650. // Add this object to the cross-referencer if requested.
  2651. if (parameters->options().user_set_print_symbol_counts()
  2652. || parameters->options().cref())
  2653. {
  2654. if (this->cref_ == NULL)
  2655. this->cref_ = new Cref();
  2656. this->cref_->add_object(obj);
  2657. }
  2658. return true;
  2659. }
  2660. // For each dynamic object, record whether we've seen all of its
  2661. // explicit dependencies.
  2662. void
  2663. Input_objects::check_dynamic_dependencies() const
  2664. {
  2665. bool issued_copy_dt_needed_error = false;
  2666. for (Dynobj_list::const_iterator p = this->dynobj_list_.begin();
  2667. p != this->dynobj_list_.end();
  2668. ++p)
  2669. {
  2670. const Dynobj::Needed& needed((*p)->needed());
  2671. bool found_all = true;
  2672. Dynobj::Needed::const_iterator pneeded;
  2673. for (pneeded = needed.begin(); pneeded != needed.end(); ++pneeded)
  2674. {
  2675. if (this->sonames_.find(*pneeded) == this->sonames_.end())
  2676. {
  2677. found_all = false;
  2678. break;
  2679. }
  2680. }
  2681. (*p)->set_has_unknown_needed_entries(!found_all);
  2682. // --copy-dt-needed-entries aka --add-needed is a GNU ld option
  2683. // that gold does not support. However, they cause no trouble
  2684. // unless there is a DT_NEEDED entry that we don't know about;
  2685. // warn only in that case.
  2686. if (!found_all
  2687. && !issued_copy_dt_needed_error
  2688. && (parameters->options().copy_dt_needed_entries()
  2689. || parameters->options().add_needed()))
  2690. {
  2691. const char* optname;
  2692. if (parameters->options().copy_dt_needed_entries())
  2693. optname = "--copy-dt-needed-entries";
  2694. else
  2695. optname = "--add-needed";
  2696. gold_error(_("%s is not supported but is required for %s in %s"),
  2697. optname, (*pneeded).c_str(), (*p)->name().c_str());
  2698. issued_copy_dt_needed_error = true;
  2699. }
  2700. }
  2701. }
  2702. // Start processing an archive.
  2703. void
  2704. Input_objects::archive_start(Archive* archive)
  2705. {
  2706. if (parameters->options().user_set_print_symbol_counts()
  2707. || parameters->options().cref())
  2708. {
  2709. if (this->cref_ == NULL)
  2710. this->cref_ = new Cref();
  2711. this->cref_->add_archive_start(archive);
  2712. }
  2713. }
  2714. // Stop processing an archive.
  2715. void
  2716. Input_objects::archive_stop(Archive* archive)
  2717. {
  2718. if (parameters->options().user_set_print_symbol_counts()
  2719. || parameters->options().cref())
  2720. this->cref_->add_archive_stop(archive);
  2721. }
  2722. // Print symbol counts
  2723. void
  2724. Input_objects::print_symbol_counts(const Symbol_table* symtab) const
  2725. {
  2726. if (parameters->options().user_set_print_symbol_counts()
  2727. && this->cref_ != NULL)
  2728. this->cref_->print_symbol_counts(symtab);
  2729. }
  2730. // Print a cross reference table.
  2731. void
  2732. Input_objects::print_cref(const Symbol_table* symtab, FILE* f) const
  2733. {
  2734. if (parameters->options().cref() && this->cref_ != NULL)
  2735. this->cref_->print_cref(symtab, f);
  2736. }
  2737. // Relocate_info methods.
  2738. // Return a string describing the location of a relocation when file
  2739. // and lineno information is not available. This is only used in
  2740. // error messages.
  2741. template<int size, bool big_endian>
  2742. std::string
  2743. Relocate_info<size, big_endian>::location(size_t, off_t offset) const
  2744. {
  2745. Sized_dwarf_line_info<size, big_endian> line_info(this->object);
  2746. std::string ret = line_info.addr2line(this->data_shndx, offset, NULL);
  2747. if (!ret.empty())
  2748. return ret;
  2749. ret = this->object->name();
  2750. Symbol_location_info info;
  2751. if (this->object->get_symbol_location_info(this->data_shndx, offset, &info))
  2752. {
  2753. if (!info.source_file.empty())
  2754. {
  2755. ret += ":";
  2756. ret += info.source_file;
  2757. }
  2758. ret += ":";
  2759. if (info.enclosing_symbol_type == elfcpp::STT_FUNC)
  2760. ret += _("function ");
  2761. ret += info.enclosing_symbol_name;
  2762. return ret;
  2763. }
  2764. ret += "(";
  2765. ret += this->object->section_name(this->data_shndx);
  2766. char buf[100];
  2767. snprintf(buf, sizeof buf, "+0x%lx)", static_cast<long>(offset));
  2768. ret += buf;
  2769. return ret;
  2770. }
  2771. } // End namespace gold.
  2772. namespace
  2773. {
  2774. using namespace gold;
  2775. // Read an ELF file with the header and return the appropriate
  2776. // instance of Object.
  2777. template<int size, bool big_endian>
  2778. Object*
  2779. make_elf_sized_object(const std::string& name, Input_file* input_file,
  2780. off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr,
  2781. bool* punconfigured)
  2782. {
  2783. Target* target = select_target(input_file, offset,
  2784. ehdr.get_e_machine(), size, big_endian,
  2785. ehdr.get_e_ident()[elfcpp::EI_OSABI],
  2786. ehdr.get_e_ident()[elfcpp::EI_ABIVERSION]);
  2787. if (target == NULL)
  2788. gold_fatal(_("%s: unsupported ELF machine number %d"),
  2789. name.c_str(), ehdr.get_e_machine());
  2790. if (!parameters->target_valid())
  2791. set_parameters_target(target);
  2792. else if (target != &parameters->target())
  2793. {
  2794. if (punconfigured != NULL)
  2795. *punconfigured = true;
  2796. else
  2797. gold_error(_("%s: incompatible target"), name.c_str());
  2798. return NULL;
  2799. }
  2800. return target->make_elf_object<size, big_endian>(name, input_file, offset,
  2801. ehdr);
  2802. }
  2803. } // End anonymous namespace.
  2804. namespace gold
  2805. {
  2806. // Return whether INPUT_FILE is an ELF object.
  2807. bool
  2808. is_elf_object(Input_file* input_file, off_t offset,
  2809. const unsigned char** start, int* read_size)
  2810. {
  2811. off_t filesize = input_file->file().filesize();
  2812. int want = elfcpp::Elf_recognizer::max_header_size;
  2813. if (filesize - offset < want)
  2814. want = filesize - offset;
  2815. const unsigned char* p = input_file->file().get_view(offset, 0, want,
  2816. true, false);
  2817. *start = p;
  2818. *read_size = want;
  2819. return elfcpp::Elf_recognizer::is_elf_file(p, want);
  2820. }
  2821. // Read an ELF file and return the appropriate instance of Object.
  2822. Object*
  2823. make_elf_object(const std::string& name, Input_file* input_file, off_t offset,
  2824. const unsigned char* p, section_offset_type bytes,
  2825. bool* punconfigured)
  2826. {
  2827. if (punconfigured != NULL)
  2828. *punconfigured = false;
  2829. std::string error;
  2830. bool big_endian = false;
  2831. int size = 0;
  2832. if (!elfcpp::Elf_recognizer::is_valid_header(p, bytes, &size,
  2833. &big_endian, &error))
  2834. {
  2835. gold_error(_("%s: %s"), name.c_str(), error.c_str());
  2836. return NULL;
  2837. }
  2838. if (size == 32)
  2839. {
  2840. if (big_endian)
  2841. {
  2842. #ifdef HAVE_TARGET_32_BIG
  2843. elfcpp::Ehdr<32, true> ehdr(p);
  2844. return make_elf_sized_object<32, true>(name, input_file,
  2845. offset, ehdr, punconfigured);
  2846. #else
  2847. if (punconfigured != NULL)
  2848. *punconfigured = true;
  2849. else
  2850. gold_error(_("%s: not configured to support "
  2851. "32-bit big-endian object"),
  2852. name.c_str());
  2853. return NULL;
  2854. #endif
  2855. }
  2856. else
  2857. {
  2858. #ifdef HAVE_TARGET_32_LITTLE
  2859. elfcpp::Ehdr<32, false> ehdr(p);
  2860. return make_elf_sized_object<32, false>(name, input_file,
  2861. offset, ehdr, punconfigured);
  2862. #else
  2863. if (punconfigured != NULL)
  2864. *punconfigured = true;
  2865. else
  2866. gold_error(_("%s: not configured to support "
  2867. "32-bit little-endian object"),
  2868. name.c_str());
  2869. return NULL;
  2870. #endif
  2871. }
  2872. }
  2873. else if (size == 64)
  2874. {
  2875. if (big_endian)
  2876. {
  2877. #ifdef HAVE_TARGET_64_BIG
  2878. elfcpp::Ehdr<64, true> ehdr(p);
  2879. return make_elf_sized_object<64, true>(name, input_file,
  2880. offset, ehdr, punconfigured);
  2881. #else
  2882. if (punconfigured != NULL)
  2883. *punconfigured = true;
  2884. else
  2885. gold_error(_("%s: not configured to support "
  2886. "64-bit big-endian object"),
  2887. name.c_str());
  2888. return NULL;
  2889. #endif
  2890. }
  2891. else
  2892. {
  2893. #ifdef HAVE_TARGET_64_LITTLE
  2894. elfcpp::Ehdr<64, false> ehdr(p);
  2895. return make_elf_sized_object<64, false>(name, input_file,
  2896. offset, ehdr, punconfigured);
  2897. #else
  2898. if (punconfigured != NULL)
  2899. *punconfigured = true;
  2900. else
  2901. gold_error(_("%s: not configured to support "
  2902. "64-bit little-endian object"),
  2903. name.c_str());
  2904. return NULL;
  2905. #endif
  2906. }
  2907. }
  2908. else
  2909. gold_unreachable();
  2910. }
  2911. // Instantiate the templates we need.
  2912. #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
  2913. template
  2914. void
  2915. Relobj::initialize_input_to_output_map<64>(unsigned int shndx,
  2916. elfcpp::Elf_types<64>::Elf_Addr starting_address,
  2917. Unordered_map<section_offset_type,
  2918. elfcpp::Elf_types<64>::Elf_Addr>* output_addresses) const;
  2919. #endif
  2920. #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
  2921. template
  2922. void
  2923. Relobj::initialize_input_to_output_map<32>(unsigned int shndx,
  2924. elfcpp::Elf_types<32>::Elf_Addr starting_address,
  2925. Unordered_map<section_offset_type,
  2926. elfcpp::Elf_types<32>::Elf_Addr>* output_addresses) const;
  2927. #endif
  2928. #ifdef HAVE_TARGET_32_LITTLE
  2929. template
  2930. void
  2931. Object::read_section_data<32, false>(elfcpp::Elf_file<32, false, Object>*,
  2932. Read_symbols_data*);
  2933. template
  2934. const unsigned char*
  2935. Object::find_shdr<32,false>(const unsigned char*, const char*, const char*,
  2936. section_size_type, const unsigned char*) const;
  2937. #endif
  2938. #ifdef HAVE_TARGET_32_BIG
  2939. template
  2940. void
  2941. Object::read_section_data<32, true>(elfcpp::Elf_file<32, true, Object>*,
  2942. Read_symbols_data*);
  2943. template
  2944. const unsigned char*
  2945. Object::find_shdr<32,true>(const unsigned char*, const char*, const char*,
  2946. section_size_type, const unsigned char*) const;
  2947. #endif
  2948. #ifdef HAVE_TARGET_64_LITTLE
  2949. template
  2950. void
  2951. Object::read_section_data<64, false>(elfcpp::Elf_file<64, false, Object>*,
  2952. Read_symbols_data*);
  2953. template
  2954. const unsigned char*
  2955. Object::find_shdr<64,false>(const unsigned char*, const char*, const char*,
  2956. section_size_type, const unsigned char*) const;
  2957. #endif
  2958. #ifdef HAVE_TARGET_64_BIG
  2959. template
  2960. void
  2961. Object::read_section_data<64, true>(elfcpp::Elf_file<64, true, Object>*,
  2962. Read_symbols_data*);
  2963. template
  2964. const unsigned char*
  2965. Object::find_shdr<64,true>(const unsigned char*, const char*, const char*,
  2966. section_size_type, const unsigned char*) const;
  2967. #endif
  2968. #ifdef HAVE_TARGET_32_LITTLE
  2969. template
  2970. class Sized_relobj<32, false>;
  2971. template
  2972. class Sized_relobj_file<32, false>;
  2973. #endif
  2974. #ifdef HAVE_TARGET_32_BIG
  2975. template
  2976. class Sized_relobj<32, true>;
  2977. template
  2978. class Sized_relobj_file<32, true>;
  2979. #endif
  2980. #ifdef HAVE_TARGET_64_LITTLE
  2981. template
  2982. class Sized_relobj<64, false>;
  2983. template
  2984. class Sized_relobj_file<64, false>;
  2985. #endif
  2986. #ifdef HAVE_TARGET_64_BIG
  2987. template
  2988. class Sized_relobj<64, true>;
  2989. template
  2990. class Sized_relobj_file<64, true>;
  2991. #endif
  2992. #ifdef HAVE_TARGET_32_LITTLE
  2993. template
  2994. struct Relocate_info<32, false>;
  2995. #endif
  2996. #ifdef HAVE_TARGET_32_BIG
  2997. template
  2998. struct Relocate_info<32, true>;
  2999. #endif
  3000. #ifdef HAVE_TARGET_64_LITTLE
  3001. template
  3002. struct Relocate_info<64, false>;
  3003. #endif
  3004. #ifdef HAVE_TARGET_64_BIG
  3005. template
  3006. struct Relocate_info<64, true>;
  3007. #endif
  3008. #ifdef HAVE_TARGET_32_LITTLE
  3009. template
  3010. void
  3011. Xindex::initialize_symtab_xindex<32, false>(Object*, unsigned int);
  3012. template
  3013. void
  3014. Xindex::read_symtab_xindex<32, false>(Object*, unsigned int,
  3015. const unsigned char*);
  3016. #endif
  3017. #ifdef HAVE_TARGET_32_BIG
  3018. template
  3019. void
  3020. Xindex::initialize_symtab_xindex<32, true>(Object*, unsigned int);
  3021. template
  3022. void
  3023. Xindex::read_symtab_xindex<32, true>(Object*, unsigned int,
  3024. const unsigned char*);
  3025. #endif
  3026. #ifdef HAVE_TARGET_64_LITTLE
  3027. template
  3028. void
  3029. Xindex::initialize_symtab_xindex<64, false>(Object*, unsigned int);
  3030. template
  3031. void
  3032. Xindex::read_symtab_xindex<64, false>(Object*, unsigned int,
  3033. const unsigned char*);
  3034. #endif
  3035. #ifdef HAVE_TARGET_64_BIG
  3036. template
  3037. void
  3038. Xindex::initialize_symtab_xindex<64, true>(Object*, unsigned int);
  3039. template
  3040. void
  3041. Xindex::read_symtab_xindex<64, true>(Object*, unsigned int,
  3042. const unsigned char*);
  3043. #endif
  3044. } // End namespace gold.