object.cc 111 KB

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