incremental.cc 97 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134
  1. // inremental.cc -- incremental linking support for gold
  2. // Copyright (C) 2009-2022 Free Software Foundation, Inc.
  3. // Written by Mikolaj Zalewski <mikolajz@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 <set>
  19. #include <cstdarg>
  20. #include "libiberty.h"
  21. #include "elfcpp.h"
  22. #include "options.h"
  23. #include "output.h"
  24. #include "symtab.h"
  25. #include "incremental.h"
  26. #include "archive.h"
  27. #include "object.h"
  28. #include "target-select.h"
  29. #include "target.h"
  30. #include "fileread.h"
  31. #include "script.h"
  32. namespace gold {
  33. // Version number for the .gnu_incremental_inputs section.
  34. // Version 1 was the initial checkin.
  35. // Version 2 adds some padding to ensure 8-byte alignment where necessary.
  36. const unsigned int INCREMENTAL_LINK_VERSION = 2;
  37. // This class manages the .gnu_incremental_inputs section, which holds
  38. // the header information, a directory of input files, and separate
  39. // entries for each input file.
  40. template<int size, bool big_endian>
  41. class Output_section_incremental_inputs : public Output_section_data
  42. {
  43. public:
  44. Output_section_incremental_inputs(const Incremental_inputs* inputs,
  45. const Symbol_table* symtab)
  46. : Output_section_data(size / 8), inputs_(inputs), symtab_(symtab)
  47. { }
  48. protected:
  49. // This is called to update the section size prior to assigning
  50. // the address and file offset.
  51. void
  52. update_data_size()
  53. { this->set_final_data_size(); }
  54. // Set the final data size.
  55. void
  56. set_final_data_size();
  57. // Write the data to the file.
  58. void
  59. do_write(Output_file*);
  60. // Write to a map file.
  61. void
  62. do_print_to_mapfile(Mapfile* mapfile) const
  63. { mapfile->print_output_data(this, _("** incremental_inputs")); }
  64. private:
  65. // Write the section header.
  66. unsigned char*
  67. write_header(unsigned char* pov, unsigned int input_file_count,
  68. section_offset_type command_line_offset);
  69. // Write the input file entries.
  70. unsigned char*
  71. write_input_files(unsigned char* oview, unsigned char* pov,
  72. Stringpool* strtab);
  73. // Write the supplemental information blocks.
  74. unsigned char*
  75. write_info_blocks(unsigned char* oview, unsigned char* pov,
  76. Stringpool* strtab, unsigned int* global_syms,
  77. unsigned int global_sym_count);
  78. // Write the contents of the .gnu_incremental_symtab section.
  79. void
  80. write_symtab(unsigned char* pov, unsigned int* global_syms,
  81. unsigned int global_sym_count);
  82. // Write the contents of the .gnu_incremental_got_plt section.
  83. void
  84. write_got_plt(unsigned char* pov, off_t view_size);
  85. // Typedefs for writing the data to the output sections.
  86. typedef elfcpp::Swap<size, big_endian> Swap;
  87. typedef elfcpp::Swap<16, big_endian> Swap16;
  88. typedef elfcpp::Swap<32, big_endian> Swap32;
  89. typedef elfcpp::Swap<64, big_endian> Swap64;
  90. // Sizes of various structures.
  91. static const int sizeof_addr = size / 8;
  92. static const int header_size =
  93. Incremental_inputs_reader<size, big_endian>::header_size;
  94. static const int input_entry_size =
  95. Incremental_inputs_reader<size, big_endian>::input_entry_size;
  96. static const unsigned int object_info_size =
  97. Incremental_inputs_reader<size, big_endian>::object_info_size;
  98. static const unsigned int input_section_entry_size =
  99. Incremental_inputs_reader<size, big_endian>::input_section_entry_size;
  100. static const unsigned int global_sym_entry_size =
  101. Incremental_inputs_reader<size, big_endian>::global_sym_entry_size;
  102. static const unsigned int incr_reloc_size =
  103. Incremental_relocs_reader<size, big_endian>::reloc_size;
  104. // The Incremental_inputs object.
  105. const Incremental_inputs* inputs_;
  106. // The symbol table.
  107. const Symbol_table* symtab_;
  108. };
  109. // Inform the user why we don't do an incremental link. Not called in
  110. // the obvious case of missing output file. TODO: Is this helpful?
  111. void
  112. vexplain_no_incremental(const char* format, va_list args)
  113. {
  114. char* buf = NULL;
  115. if (vasprintf(&buf, format, args) < 0)
  116. gold_nomem();
  117. gold_info(_("the link might take longer: "
  118. "cannot perform incremental link: %s"), buf);
  119. free(buf);
  120. }
  121. void
  122. explain_no_incremental(const char* format, ...)
  123. {
  124. va_list args;
  125. va_start(args, format);
  126. vexplain_no_incremental(format, args);
  127. va_end(args);
  128. }
  129. // Report an error.
  130. void
  131. Incremental_binary::error(const char* format, ...) const
  132. {
  133. va_list args;
  134. va_start(args, format);
  135. // Current code only checks if the file can be used for incremental linking,
  136. // so errors shouldn't fail the build, but only result in a fallback to a
  137. // full build.
  138. // TODO: when we implement incremental editing of the file, we may need a
  139. // flag that will cause errors to be treated seriously.
  140. vexplain_no_incremental(format, args);
  141. va_end(args);
  142. }
  143. // Return TRUE if a section of type SH_TYPE can be updated in place
  144. // during an incremental update. We can update sections of type PROGBITS,
  145. // NOBITS, INIT_ARRAY, FINI_ARRAY, PREINIT_ARRAY, NOTE, and
  146. // (processor-specific) unwind sections. All others will be regenerated.
  147. bool
  148. can_incremental_update(unsigned int sh_type)
  149. {
  150. return (sh_type == elfcpp::SHT_PROGBITS
  151. || sh_type == elfcpp::SHT_NOBITS
  152. || sh_type == elfcpp::SHT_INIT_ARRAY
  153. || sh_type == elfcpp::SHT_FINI_ARRAY
  154. || sh_type == elfcpp::SHT_PREINIT_ARRAY
  155. || sh_type == elfcpp::SHT_NOTE
  156. || sh_type == parameters->target().unwind_section_type());
  157. }
  158. // Find the .gnu_incremental_inputs section and related sections.
  159. template<int size, bool big_endian>
  160. bool
  161. Sized_incremental_binary<size, big_endian>::find_incremental_inputs_sections(
  162. unsigned int* p_inputs_shndx,
  163. unsigned int* p_symtab_shndx,
  164. unsigned int* p_relocs_shndx,
  165. unsigned int* p_got_plt_shndx,
  166. unsigned int* p_strtab_shndx)
  167. {
  168. unsigned int inputs_shndx =
  169. this->elf_file_.find_section_by_type(elfcpp::SHT_GNU_INCREMENTAL_INPUTS);
  170. if (inputs_shndx == elfcpp::SHN_UNDEF) // Not found.
  171. return false;
  172. unsigned int symtab_shndx =
  173. this->elf_file_.find_section_by_type(elfcpp::SHT_GNU_INCREMENTAL_SYMTAB);
  174. if (symtab_shndx == elfcpp::SHN_UNDEF) // Not found.
  175. return false;
  176. if (this->elf_file_.section_link(symtab_shndx) != inputs_shndx)
  177. return false;
  178. unsigned int relocs_shndx =
  179. this->elf_file_.find_section_by_type(elfcpp::SHT_GNU_INCREMENTAL_RELOCS);
  180. if (relocs_shndx == elfcpp::SHN_UNDEF) // Not found.
  181. return false;
  182. if (this->elf_file_.section_link(relocs_shndx) != inputs_shndx)
  183. return false;
  184. unsigned int got_plt_shndx =
  185. this->elf_file_.find_section_by_type(elfcpp::SHT_GNU_INCREMENTAL_GOT_PLT);
  186. if (got_plt_shndx == elfcpp::SHN_UNDEF) // Not found.
  187. return false;
  188. if (this->elf_file_.section_link(got_plt_shndx) != inputs_shndx)
  189. return false;
  190. unsigned int strtab_shndx = this->elf_file_.section_link(inputs_shndx);
  191. if (strtab_shndx == elfcpp::SHN_UNDEF
  192. || strtab_shndx > this->elf_file_.shnum()
  193. || this->elf_file_.section_type(strtab_shndx) != elfcpp::SHT_STRTAB)
  194. return false;
  195. if (p_inputs_shndx != NULL)
  196. *p_inputs_shndx = inputs_shndx;
  197. if (p_symtab_shndx != NULL)
  198. *p_symtab_shndx = symtab_shndx;
  199. if (p_relocs_shndx != NULL)
  200. *p_relocs_shndx = relocs_shndx;
  201. if (p_got_plt_shndx != NULL)
  202. *p_got_plt_shndx = got_plt_shndx;
  203. if (p_strtab_shndx != NULL)
  204. *p_strtab_shndx = strtab_shndx;
  205. return true;
  206. }
  207. // Set up the readers into the incremental info sections.
  208. template<int size, bool big_endian>
  209. void
  210. Sized_incremental_binary<size, big_endian>::setup_readers()
  211. {
  212. unsigned int inputs_shndx;
  213. unsigned int symtab_shndx;
  214. unsigned int relocs_shndx;
  215. unsigned int got_plt_shndx;
  216. unsigned int strtab_shndx;
  217. if (!this->find_incremental_inputs_sections(&inputs_shndx, &symtab_shndx,
  218. &relocs_shndx, &got_plt_shndx,
  219. &strtab_shndx))
  220. return;
  221. Location inputs_location(this->elf_file_.section_contents(inputs_shndx));
  222. Location symtab_location(this->elf_file_.section_contents(symtab_shndx));
  223. Location relocs_location(this->elf_file_.section_contents(relocs_shndx));
  224. Location got_plt_location(this->elf_file_.section_contents(got_plt_shndx));
  225. Location strtab_location(this->elf_file_.section_contents(strtab_shndx));
  226. View inputs_view = this->view(inputs_location);
  227. View symtab_view = this->view(symtab_location);
  228. View relocs_view = this->view(relocs_location);
  229. View got_plt_view = this->view(got_plt_location);
  230. View strtab_view = this->view(strtab_location);
  231. elfcpp::Elf_strtab strtab(strtab_view.data(), strtab_location.data_size);
  232. this->inputs_reader_ =
  233. Incremental_inputs_reader<size, big_endian>(inputs_view.data(), strtab);
  234. this->symtab_reader_ =
  235. Incremental_symtab_reader<big_endian>(symtab_view.data(),
  236. symtab_location.data_size);
  237. this->relocs_reader_ =
  238. Incremental_relocs_reader<size, big_endian>(relocs_view.data(),
  239. relocs_location.data_size);
  240. this->got_plt_reader_ =
  241. Incremental_got_plt_reader<big_endian>(got_plt_view.data());
  242. // Find the main symbol table.
  243. unsigned int main_symtab_shndx =
  244. this->elf_file_.find_section_by_type(elfcpp::SHT_SYMTAB);
  245. gold_assert(main_symtab_shndx != elfcpp::SHN_UNDEF);
  246. this->main_symtab_loc_ = this->elf_file_.section_contents(main_symtab_shndx);
  247. // Find the main symbol string table.
  248. unsigned int main_strtab_shndx =
  249. this->elf_file_.section_link(main_symtab_shndx);
  250. gold_assert(main_strtab_shndx != elfcpp::SHN_UNDEF
  251. && main_strtab_shndx < this->elf_file_.shnum());
  252. this->main_strtab_loc_ = this->elf_file_.section_contents(main_strtab_shndx);
  253. // Walk the list of input files (a) to setup an Input_reader for each
  254. // input file, and (b) to record maps of files added from archive
  255. // libraries and scripts.
  256. Incremental_inputs_reader<size, big_endian>& inputs = this->inputs_reader_;
  257. unsigned int count = inputs.input_file_count();
  258. this->input_objects_.resize(count);
  259. this->input_entry_readers_.reserve(count);
  260. this->library_map_.resize(count);
  261. this->script_map_.resize(count);
  262. for (unsigned int i = 0; i < count; i++)
  263. {
  264. Input_entry_reader input_file = inputs.input_file(i);
  265. #if __cplusplus >= 2001103L
  266. this->input_entry_readers_.emplace_back(input_file);
  267. #else
  268. this->input_entry_readers_.push_back(Sized_input_reader(input_file));
  269. #endif
  270. switch (input_file.type())
  271. {
  272. case INCREMENTAL_INPUT_OBJECT:
  273. case INCREMENTAL_INPUT_ARCHIVE_MEMBER:
  274. case INCREMENTAL_INPUT_SHARED_LIBRARY:
  275. // No special treatment necessary.
  276. break;
  277. case INCREMENTAL_INPUT_ARCHIVE:
  278. {
  279. Incremental_library* lib =
  280. new Incremental_library(input_file.filename(), i,
  281. &this->input_entry_readers_[i]);
  282. this->library_map_[i] = lib;
  283. unsigned int member_count = input_file.get_member_count();
  284. for (unsigned int j = 0; j < member_count; j++)
  285. {
  286. int member_offset = input_file.get_member_offset(j);
  287. int member_index = inputs.input_file_index(member_offset);
  288. this->library_map_[member_index] = lib;
  289. }
  290. }
  291. break;
  292. case INCREMENTAL_INPUT_SCRIPT:
  293. {
  294. Script_info* script = new Script_info(input_file.filename(), i);
  295. this->script_map_[i] = script;
  296. unsigned int object_count = input_file.get_object_count();
  297. for (unsigned int j = 0; j < object_count; j++)
  298. {
  299. int object_offset = input_file.get_object_offset(j);
  300. int object_index = inputs.input_file_index(object_offset);
  301. this->script_map_[object_index] = script;
  302. }
  303. }
  304. break;
  305. default:
  306. gold_unreachable();
  307. }
  308. }
  309. // Initialize the map of global symbols.
  310. unsigned int nglobals = this->symtab_reader_.symbol_count();
  311. this->symbol_map_.resize(nglobals);
  312. this->has_incremental_info_ = true;
  313. }
  314. // Walk the list of input files given on the command line, and build
  315. // a direct map of file index to the corresponding input argument.
  316. void
  317. check_input_args(std::vector<const Input_argument*>& input_args_map,
  318. Input_arguments::const_iterator begin,
  319. Input_arguments::const_iterator end)
  320. {
  321. for (Input_arguments::const_iterator p = begin;
  322. p != end;
  323. ++p)
  324. {
  325. if (p->is_group())
  326. {
  327. const Input_file_group* group = p->group();
  328. check_input_args(input_args_map, group->begin(), group->end());
  329. }
  330. else if (p->is_lib())
  331. {
  332. const Input_file_lib* lib = p->lib();
  333. check_input_args(input_args_map, lib->begin(), lib->end());
  334. }
  335. else
  336. {
  337. gold_assert(p->is_file());
  338. unsigned int arg_serial = p->file().arg_serial();
  339. if (arg_serial > 0)
  340. {
  341. gold_assert(arg_serial <= input_args_map.size());
  342. gold_assert(input_args_map[arg_serial - 1] == 0);
  343. input_args_map[arg_serial - 1] = &*p;
  344. }
  345. }
  346. }
  347. }
  348. // Determine whether an incremental link based on the existing output file
  349. // can be done.
  350. template<int size, bool big_endian>
  351. bool
  352. Sized_incremental_binary<size, big_endian>::do_check_inputs(
  353. const Command_line& cmdline,
  354. Incremental_inputs* incremental_inputs)
  355. {
  356. Incremental_inputs_reader<size, big_endian>& inputs = this->inputs_reader_;
  357. if (!this->has_incremental_info_)
  358. {
  359. explain_no_incremental(_("no incremental data from previous build"));
  360. return false;
  361. }
  362. if (inputs.version() != INCREMENTAL_LINK_VERSION)
  363. {
  364. explain_no_incremental(_("different version of incremental build data"));
  365. return false;
  366. }
  367. if (incremental_inputs->command_line() != inputs.command_line())
  368. {
  369. gold_debug(DEBUG_INCREMENTAL,
  370. "old command line: %s",
  371. inputs.command_line());
  372. gold_debug(DEBUG_INCREMENTAL,
  373. "new command line: %s",
  374. incremental_inputs->command_line().c_str());
  375. explain_no_incremental(_("command line changed"));
  376. return false;
  377. }
  378. // Walk the list of input files given on the command line, and build
  379. // a direct map of argument serial numbers to the corresponding input
  380. // arguments.
  381. this->input_args_map_.resize(cmdline.number_of_input_files());
  382. check_input_args(this->input_args_map_, cmdline.begin(), cmdline.end());
  383. // Walk the list of input files to check for conditions that prevent
  384. // an incremental update link.
  385. unsigned int count = inputs.input_file_count();
  386. for (unsigned int i = 0; i < count; i++)
  387. {
  388. Input_entry_reader input_file = inputs.input_file(i);
  389. switch (input_file.type())
  390. {
  391. case INCREMENTAL_INPUT_OBJECT:
  392. case INCREMENTAL_INPUT_ARCHIVE_MEMBER:
  393. case INCREMENTAL_INPUT_SHARED_LIBRARY:
  394. case INCREMENTAL_INPUT_ARCHIVE:
  395. // No special treatment necessary.
  396. break;
  397. case INCREMENTAL_INPUT_SCRIPT:
  398. if (this->do_file_has_changed(i))
  399. {
  400. explain_no_incremental(_("%s: script file changed"),
  401. input_file.filename());
  402. return false;
  403. }
  404. break;
  405. default:
  406. gold_unreachable();
  407. }
  408. }
  409. return true;
  410. }
  411. // Return TRUE if input file N has changed since the last incremental link.
  412. template<int size, bool big_endian>
  413. bool
  414. Sized_incremental_binary<size, big_endian>::do_file_has_changed(
  415. unsigned int n) const
  416. {
  417. Input_entry_reader input_file = this->inputs_reader_.input_file(n);
  418. Incremental_disposition disp = INCREMENTAL_CHECK;
  419. // For files named in scripts, find the file that was actually named
  420. // on the command line, so that we can get the incremental disposition
  421. // flag.
  422. Script_info* script = this->get_script_info(n);
  423. if (script != NULL)
  424. n = script->input_file_index();
  425. const Input_argument* input_argument = this->get_input_argument(n);
  426. if (input_argument != NULL)
  427. disp = input_argument->file().options().incremental_disposition();
  428. // For files at the beginning of the command line (i.e., those added
  429. // implicitly by gcc), check whether the --incremental-startup-unchanged
  430. // option was used.
  431. if (disp == INCREMENTAL_STARTUP)
  432. disp = parameters->options().incremental_startup_disposition();
  433. if (disp != INCREMENTAL_CHECK)
  434. return disp == INCREMENTAL_CHANGED;
  435. const char* filename = input_file.filename();
  436. Timespec old_mtime = input_file.get_mtime();
  437. Timespec new_mtime;
  438. if (!get_mtime(filename, &new_mtime))
  439. {
  440. // If we can't open get the current modification time, assume it has
  441. // changed. If the file doesn't exist, we'll issue an error when we
  442. // try to open it later.
  443. return true;
  444. }
  445. if (new_mtime.seconds > old_mtime.seconds)
  446. return true;
  447. if (new_mtime.seconds == old_mtime.seconds
  448. && new_mtime.nanoseconds > old_mtime.nanoseconds)
  449. return true;
  450. return false;
  451. }
  452. // Initialize the layout of the output file based on the existing
  453. // output file.
  454. template<int size, bool big_endian>
  455. void
  456. Sized_incremental_binary<size, big_endian>::do_init_layout(Layout* layout)
  457. {
  458. typedef elfcpp::Shdr<size, big_endian> Shdr;
  459. const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
  460. // Get views of the section headers and the section string table.
  461. const off_t shoff = this->elf_file_.shoff();
  462. const unsigned int shnum = this->elf_file_.shnum();
  463. const unsigned int shstrndx = this->elf_file_.shstrndx();
  464. Location shdrs_location(shoff, shnum * shdr_size);
  465. Location shstrndx_location(this->elf_file_.section_contents(shstrndx));
  466. View shdrs_view = this->view(shdrs_location);
  467. View shstrndx_view = this->view(shstrndx_location);
  468. elfcpp::Elf_strtab shstrtab(shstrndx_view.data(),
  469. shstrndx_location.data_size);
  470. layout->set_incremental_base(this);
  471. // Initialize the layout.
  472. this->section_map_.resize(shnum);
  473. const unsigned char* pshdr = shdrs_view.data() + shdr_size;
  474. for (unsigned int i = 1; i < shnum; i++)
  475. {
  476. Shdr shdr(pshdr);
  477. const char* name;
  478. if (!shstrtab.get_c_string(shdr.get_sh_name(), &name))
  479. name = NULL;
  480. gold_debug(DEBUG_INCREMENTAL,
  481. "Output section: %2d %08lx %08lx %08lx %3d %s",
  482. i,
  483. static_cast<long>(shdr.get_sh_addr()),
  484. static_cast<long>(shdr.get_sh_offset()),
  485. static_cast<long>(shdr.get_sh_size()),
  486. shdr.get_sh_type(), name ? name : "<null>");
  487. this->section_map_[i] = layout->init_fixed_output_section(name, shdr);
  488. pshdr += shdr_size;
  489. }
  490. }
  491. // Mark regions of the input file that must be kept unchanged.
  492. template<int size, bool big_endian>
  493. void
  494. Sized_incremental_binary<size, big_endian>::do_reserve_layout(
  495. unsigned int input_file_index)
  496. {
  497. const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
  498. Input_entry_reader input_file =
  499. this->inputs_reader_.input_file(input_file_index);
  500. if (input_file.type() == INCREMENTAL_INPUT_SHARED_LIBRARY)
  501. {
  502. // Reserve the BSS space used for COPY relocations.
  503. unsigned int nsyms = input_file.get_global_symbol_count();
  504. Incremental_binary::View symtab_view(NULL);
  505. unsigned int symtab_count;
  506. elfcpp::Elf_strtab strtab(NULL, 0);
  507. this->get_symtab_view(&symtab_view, &symtab_count, &strtab);
  508. for (unsigned int i = 0; i < nsyms; ++i)
  509. {
  510. bool is_def;
  511. bool is_copy;
  512. unsigned int output_symndx =
  513. input_file.get_output_symbol_index(i, &is_def, &is_copy);
  514. if (is_copy)
  515. {
  516. const unsigned char* sym_p = (symtab_view.data()
  517. + output_symndx * sym_size);
  518. elfcpp::Sym<size, big_endian> gsym(sym_p);
  519. unsigned int shndx = gsym.get_st_shndx();
  520. if (shndx < 1 || shndx >= this->section_map_.size())
  521. continue;
  522. Output_section* os = this->section_map_[shndx];
  523. off_t offset = gsym.get_st_value() - os->address();
  524. os->reserve(offset, gsym.get_st_size());
  525. gold_debug(DEBUG_INCREMENTAL,
  526. "Reserve for COPY reloc: %s, off %d, size %d",
  527. os->name(),
  528. static_cast<int>(offset),
  529. static_cast<int>(gsym.get_st_size()));
  530. }
  531. }
  532. return;
  533. }
  534. unsigned int shnum = input_file.get_input_section_count();
  535. for (unsigned int i = 0; i < shnum; i++)
  536. {
  537. typename Input_entry_reader::Input_section_info sect =
  538. input_file.get_input_section(i);
  539. if (sect.output_shndx == 0 || sect.sh_offset == -1)
  540. continue;
  541. Output_section* os = this->section_map_[sect.output_shndx];
  542. gold_assert(os != NULL);
  543. os->reserve(sect.sh_offset, sect.sh_size);
  544. }
  545. }
  546. // Process the GOT and PLT entries from the existing output file.
  547. template<int size, bool big_endian>
  548. void
  549. Sized_incremental_binary<size, big_endian>::do_process_got_plt(
  550. Symbol_table* symtab,
  551. Layout* layout)
  552. {
  553. Incremental_got_plt_reader<big_endian> got_plt_reader(this->got_plt_reader());
  554. Sized_target<size, big_endian>* target =
  555. parameters->sized_target<size, big_endian>();
  556. // Get the number of symbols in the main symbol table and in the
  557. // incremental symbol table. The difference between the two counts
  558. // is the index of the first forced-local or global symbol in the
  559. // main symbol table.
  560. unsigned int symtab_count =
  561. this->main_symtab_loc_.data_size / elfcpp::Elf_sizes<size>::sym_size;
  562. unsigned int isym_count = this->symtab_reader_.symbol_count();
  563. unsigned int first_global = symtab_count - isym_count;
  564. // Tell the target how big the GOT and PLT sections are.
  565. unsigned int got_count = got_plt_reader.get_got_entry_count();
  566. unsigned int plt_count = got_plt_reader.get_plt_entry_count();
  567. Output_data_got_base* got =
  568. target->init_got_plt_for_update(symtab, layout, got_count, plt_count);
  569. // Read the GOT entries from the base file and build the outgoing GOT.
  570. for (unsigned int i = 0; i < got_count; ++i)
  571. {
  572. unsigned int got_type = got_plt_reader.get_got_type(i);
  573. if ((got_type & 0x7f) == 0x7f)
  574. {
  575. // This is the second entry of a pair.
  576. got->reserve_slot(i);
  577. continue;
  578. }
  579. unsigned int symndx = got_plt_reader.get_got_symndx(i);
  580. if (got_type & 0x80)
  581. {
  582. // This is an entry for a local symbol. Ignore this entry if
  583. // the object file was replaced.
  584. unsigned int input_index = got_plt_reader.get_got_input_index(i);
  585. gold_debug(DEBUG_INCREMENTAL,
  586. "GOT entry %d, type %02x: (local symbol)",
  587. i, got_type & 0x7f);
  588. Sized_relobj_incr<size, big_endian>* obj =
  589. this->input_object(input_index);
  590. if (obj != NULL)
  591. target->reserve_local_got_entry(i, obj, symndx, got_type & 0x7f);
  592. }
  593. else
  594. {
  595. // This is an entry for a global symbol. GOT_DESC is the symbol
  596. // table index.
  597. // FIXME: This should really be a fatal error (corrupt input).
  598. gold_assert(symndx >= first_global && symndx < symtab_count);
  599. Symbol* sym = this->global_symbol(symndx - first_global);
  600. // Add the GOT entry only if the symbol is still referenced.
  601. if (sym != NULL && sym->in_reg())
  602. {
  603. gold_debug(DEBUG_INCREMENTAL,
  604. "GOT entry %d, type %02x: %s",
  605. i, got_type, sym->name());
  606. target->reserve_global_got_entry(i, sym, got_type);
  607. }
  608. }
  609. }
  610. // Read the PLT entries from the base file and pass each to the target.
  611. for (unsigned int i = 0; i < plt_count; ++i)
  612. {
  613. unsigned int plt_desc = got_plt_reader.get_plt_desc(i);
  614. // FIXME: This should really be a fatal error (corrupt input).
  615. gold_assert(plt_desc >= first_global && plt_desc < symtab_count);
  616. Symbol* sym = this->global_symbol(plt_desc - first_global);
  617. // Add the PLT entry only if the symbol is still referenced.
  618. if (sym != NULL && sym->in_reg())
  619. {
  620. gold_debug(DEBUG_INCREMENTAL,
  621. "PLT entry %d: %s",
  622. i, sym->name());
  623. target->register_global_plt_entry(symtab, layout, i, sym);
  624. }
  625. }
  626. }
  627. // Emit COPY relocations from the existing output file.
  628. template<int size, bool big_endian>
  629. void
  630. Sized_incremental_binary<size, big_endian>::do_emit_copy_relocs(
  631. Symbol_table* symtab)
  632. {
  633. Sized_target<size, big_endian>* target =
  634. parameters->sized_target<size, big_endian>();
  635. for (typename Copy_relocs::iterator p = this->copy_relocs_.begin();
  636. p != this->copy_relocs_.end();
  637. ++p)
  638. {
  639. if (!(*p).symbol->is_copied_from_dynobj())
  640. target->emit_copy_reloc(symtab, (*p).symbol, (*p).output_section,
  641. (*p).offset);
  642. }
  643. }
  644. // Apply incremental relocations for symbols whose values have changed.
  645. template<int size, bool big_endian>
  646. void
  647. Sized_incremental_binary<size, big_endian>::do_apply_incremental_relocs(
  648. const Symbol_table* symtab,
  649. Layout* layout,
  650. Output_file* of)
  651. {
  652. typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
  653. typedef typename elfcpp::Elf_types<size>::Elf_Swxword Addend;
  654. Incremental_symtab_reader<big_endian> isymtab(this->symtab_reader());
  655. Incremental_relocs_reader<size, big_endian> irelocs(this->relocs_reader());
  656. unsigned int nglobals = isymtab.symbol_count();
  657. const unsigned int incr_reloc_size = irelocs.reloc_size;
  658. Relocate_info<size, big_endian> relinfo;
  659. relinfo.symtab = symtab;
  660. relinfo.layout = layout;
  661. relinfo.object = NULL;
  662. relinfo.reloc_shndx = 0;
  663. relinfo.reloc_shdr = NULL;
  664. relinfo.data_shndx = 0;
  665. relinfo.data_shdr = NULL;
  666. Sized_target<size, big_endian>* target =
  667. parameters->sized_target<size, big_endian>();
  668. for (unsigned int i = 0; i < nglobals; i++)
  669. {
  670. const Symbol* gsym = this->global_symbol(i);
  671. // If the symbol is not referenced from any unchanged input files,
  672. // we do not need to reapply any of its relocations.
  673. if (gsym == NULL)
  674. continue;
  675. // If the symbol is defined in an unchanged file, we do not need to
  676. // reapply any of its relocations.
  677. if (gsym->source() == Symbol::FROM_OBJECT
  678. && gsym->object()->is_incremental())
  679. continue;
  680. gold_debug(DEBUG_INCREMENTAL,
  681. "Applying incremental relocations for global symbol %s [%d]",
  682. gsym->name(), i);
  683. // Follow the linked list of input symbol table entries for this symbol.
  684. // We don't bother to figure out whether the symbol table entry belongs
  685. // to a changed or unchanged file because it's easier just to apply all
  686. // the relocations -- although we might scribble over an area that has
  687. // been reallocated, we do this before copying any new data into the
  688. // output file.
  689. unsigned int offset = isymtab.get_list_head(i);
  690. while (offset > 0)
  691. {
  692. Incremental_global_symbol_reader<big_endian> sym_info =
  693. this->inputs_reader().global_symbol_reader_at_offset(offset);
  694. unsigned int r_base = sym_info.reloc_offset();
  695. unsigned int r_count = sym_info.reloc_count();
  696. // Apply each relocation for this symbol table entry.
  697. for (unsigned int j = 0; j < r_count;
  698. ++j, r_base += incr_reloc_size)
  699. {
  700. unsigned int r_type = irelocs.get_r_type(r_base);
  701. unsigned int r_shndx = irelocs.get_r_shndx(r_base);
  702. Address r_offset = irelocs.get_r_offset(r_base);
  703. Addend r_addend = irelocs.get_r_addend(r_base);
  704. Output_section* os = this->output_section(r_shndx);
  705. Address address = os->address();
  706. off_t section_offset = os->offset();
  707. size_t view_size = os->data_size();
  708. unsigned char* const view = of->get_output_view(section_offset,
  709. view_size);
  710. gold_debug(DEBUG_INCREMENTAL,
  711. " %08lx: %s + %d: type %d addend %ld",
  712. (long)(section_offset + r_offset),
  713. os->name(),
  714. (int)r_offset,
  715. r_type,
  716. (long)r_addend);
  717. target->apply_relocation(&relinfo, r_offset, r_type, r_addend,
  718. gsym, view, address, view_size);
  719. // FIXME: Do something more efficient if write_output_view
  720. // ever becomes more than a no-op.
  721. of->write_output_view(section_offset, view_size, view);
  722. }
  723. offset = sym_info.next_offset();
  724. }
  725. }
  726. }
  727. // Get a view of the main symbol table and the symbol string table.
  728. template<int size, bool big_endian>
  729. void
  730. Sized_incremental_binary<size, big_endian>::get_symtab_view(
  731. View* symtab_view,
  732. unsigned int* nsyms,
  733. elfcpp::Elf_strtab* strtab)
  734. {
  735. *symtab_view = this->view(this->main_symtab_loc_);
  736. *nsyms = this->main_symtab_loc_.data_size / elfcpp::Elf_sizes<size>::sym_size;
  737. View strtab_view(this->view(this->main_strtab_loc_));
  738. *strtab = elfcpp::Elf_strtab(strtab_view.data(),
  739. this->main_strtab_loc_.data_size);
  740. }
  741. namespace
  742. {
  743. // Create a Sized_incremental_binary object of the specified size and
  744. // endianness. Fails if the target architecture is not supported.
  745. template<int size, bool big_endian>
  746. Incremental_binary*
  747. make_sized_incremental_binary(Output_file* file,
  748. const elfcpp::Ehdr<size, big_endian>& ehdr)
  749. {
  750. Target* target = select_target(NULL, 0, // XXX
  751. ehdr.get_e_machine(), size, big_endian,
  752. ehdr.get_ei_osabi(),
  753. ehdr.get_ei_abiversion());
  754. if (target == NULL)
  755. {
  756. explain_no_incremental(_("unsupported ELF machine number %d"),
  757. ehdr.get_e_machine());
  758. return NULL;
  759. }
  760. if (!parameters->target_valid())
  761. set_parameters_target(target);
  762. else if (target != &parameters->target())
  763. gold_error(_("%s: incompatible target"), file->filename());
  764. return new Sized_incremental_binary<size, big_endian>(file, ehdr, target);
  765. }
  766. } // End of anonymous namespace.
  767. // Create an Incremental_binary object for FILE. Returns NULL is this is not
  768. // possible, e.g. FILE is not an ELF file or has an unsupported target. FILE
  769. // should be opened.
  770. Incremental_binary*
  771. open_incremental_binary(Output_file* file)
  772. {
  773. off_t filesize = file->filesize();
  774. int want = elfcpp::Elf_recognizer::max_header_size;
  775. if (filesize < want)
  776. want = filesize;
  777. const unsigned char* p = file->get_input_view(0, want);
  778. if (!elfcpp::Elf_recognizer::is_elf_file(p, want))
  779. {
  780. explain_no_incremental(_("output is not an ELF file."));
  781. return NULL;
  782. }
  783. int size = 0;
  784. bool big_endian = false;
  785. std::string error;
  786. if (!elfcpp::Elf_recognizer::is_valid_header(p, want, &size, &big_endian,
  787. &error))
  788. {
  789. explain_no_incremental(error.c_str());
  790. return NULL;
  791. }
  792. Incremental_binary* result = NULL;
  793. if (size == 32)
  794. {
  795. if (big_endian)
  796. {
  797. #ifdef HAVE_TARGET_32_BIG
  798. result = make_sized_incremental_binary<32, true>(
  799. file, elfcpp::Ehdr<32, true>(p));
  800. #else
  801. explain_no_incremental(_("unsupported file: 32-bit, big-endian"));
  802. #endif
  803. }
  804. else
  805. {
  806. #ifdef HAVE_TARGET_32_LITTLE
  807. result = make_sized_incremental_binary<32, false>(
  808. file, elfcpp::Ehdr<32, false>(p));
  809. #else
  810. explain_no_incremental(_("unsupported file: 32-bit, little-endian"));
  811. #endif
  812. }
  813. }
  814. else if (size == 64)
  815. {
  816. if (big_endian)
  817. {
  818. #ifdef HAVE_TARGET_64_BIG
  819. result = make_sized_incremental_binary<64, true>(
  820. file, elfcpp::Ehdr<64, true>(p));
  821. #else
  822. explain_no_incremental(_("unsupported file: 64-bit, big-endian"));
  823. #endif
  824. }
  825. else
  826. {
  827. #ifdef HAVE_TARGET_64_LITTLE
  828. result = make_sized_incremental_binary<64, false>(
  829. file, elfcpp::Ehdr<64, false>(p));
  830. #else
  831. explain_no_incremental(_("unsupported file: 64-bit, little-endian"));
  832. #endif
  833. }
  834. }
  835. else
  836. gold_unreachable();
  837. return result;
  838. }
  839. // Class Incremental_inputs.
  840. // Add the command line to the string table, setting
  841. // command_line_key_. In incremental builds, the command line is
  842. // stored in .gnu_incremental_inputs so that the next linker run can
  843. // check if the command line options didn't change.
  844. void
  845. Incremental_inputs::report_command_line(int argc, const char* const* argv)
  846. {
  847. // Always store 'gold' as argv[0] to avoid a full relink if the user used a
  848. // different path to the linker.
  849. std::string args("gold");
  850. // Copied from collect_argv in main.cc.
  851. for (int i = 1; i < argc; ++i)
  852. {
  853. // Adding/removing these options should not result in a full relink.
  854. if (strcmp(argv[i], "--incremental") == 0
  855. || strcmp(argv[i], "--incremental-full") == 0
  856. || strcmp(argv[i], "--incremental-update") == 0
  857. || strcmp(argv[i], "--incremental-changed") == 0
  858. || strcmp(argv[i], "--incremental-unchanged") == 0
  859. || strcmp(argv[i], "--incremental-unknown") == 0
  860. || strcmp(argv[i], "--incremental-startup-unchanged") == 0
  861. || is_prefix_of("--incremental-base=", argv[i])
  862. || is_prefix_of("--incremental-patch=", argv[i])
  863. || is_prefix_of("--debug=", argv[i]))
  864. continue;
  865. if (strcmp(argv[i], "--incremental-base") == 0
  866. || strcmp(argv[i], "--incremental-patch") == 0
  867. || strcmp(argv[i], "--debug") == 0)
  868. {
  869. // When these options are used without the '=', skip the
  870. // following parameter as well.
  871. ++i;
  872. continue;
  873. }
  874. args.append(" '");
  875. // Now append argv[i], but with all single-quotes escaped
  876. const char* argpos = argv[i];
  877. while (1)
  878. {
  879. const int len = strcspn(argpos, "'");
  880. args.append(argpos, len);
  881. if (argpos[len] == '\0')
  882. break;
  883. args.append("'\"'\"'");
  884. argpos += len + 1;
  885. }
  886. args.append("'");
  887. }
  888. this->command_line_ = args;
  889. this->strtab_->add(this->command_line_.c_str(), false,
  890. &this->command_line_key_);
  891. }
  892. // Record the input archive file ARCHIVE. This is called by the
  893. // Add_archive_symbols task before determining which archive members
  894. // to include. We create the Incremental_archive_entry here and
  895. // attach it to the Archive, but we do not add it to the list of
  896. // input objects until report_archive_end is called.
  897. void
  898. Incremental_inputs::report_archive_begin(Library_base* arch,
  899. unsigned int arg_serial,
  900. Script_info* script_info)
  901. {
  902. Stringpool::Key filename_key;
  903. Timespec mtime = arch->get_mtime();
  904. // For a file loaded from a script, don't record its argument serial number.
  905. if (script_info != NULL)
  906. arg_serial = 0;
  907. this->strtab_->add(arch->filename().c_str(), false, &filename_key);
  908. Incremental_archive_entry* entry =
  909. new Incremental_archive_entry(filename_key, arg_serial, mtime);
  910. arch->set_incremental_info(entry);
  911. if (script_info != NULL)
  912. {
  913. Incremental_script_entry* script_entry = script_info->incremental_info();
  914. gold_assert(script_entry != NULL);
  915. script_entry->add_object(entry);
  916. }
  917. }
  918. // Visitor class for processing the unused global symbols in a library.
  919. // An instance of this class is passed to the library's
  920. // for_all_unused_symbols() iterator, which will call the visit()
  921. // function for each global symbol defined in each unused library
  922. // member. We add those symbol names to the incremental info for the
  923. // library.
  924. class Unused_symbol_visitor : public Library_base::Symbol_visitor_base
  925. {
  926. public:
  927. Unused_symbol_visitor(Incremental_archive_entry* entry, Stringpool* strtab)
  928. : entry_(entry), strtab_(strtab)
  929. { }
  930. void
  931. visit(const char* sym)
  932. {
  933. Stringpool::Key symbol_key;
  934. this->strtab_->add(sym, true, &symbol_key);
  935. this->entry_->add_unused_global_symbol(symbol_key);
  936. }
  937. private:
  938. Incremental_archive_entry* entry_;
  939. Stringpool* strtab_;
  940. };
  941. // Finish recording the input archive file ARCHIVE. This is called by the
  942. // Add_archive_symbols task after determining which archive members
  943. // to include.
  944. void
  945. Incremental_inputs::report_archive_end(Library_base* arch)
  946. {
  947. Incremental_archive_entry* entry = arch->incremental_info();
  948. gold_assert(entry != NULL);
  949. this->inputs_.push_back(entry);
  950. // Collect unused global symbols.
  951. Unused_symbol_visitor v(entry, this->strtab_);
  952. arch->for_all_unused_symbols(&v);
  953. }
  954. // Record the input object file OBJ. If ARCH is not NULL, attach
  955. // the object file to the archive. This is called by the
  956. // Add_symbols task after finding out the type of the file.
  957. void
  958. Incremental_inputs::report_object(Object* obj, unsigned int arg_serial,
  959. Library_base* arch, Script_info* script_info)
  960. {
  961. Stringpool::Key filename_key;
  962. Timespec mtime = obj->get_mtime();
  963. // For a file loaded from a script, don't record its argument serial number.
  964. if (script_info != NULL)
  965. arg_serial = 0;
  966. this->strtab_->add(obj->name().c_str(), false, &filename_key);
  967. Incremental_input_entry* input_entry;
  968. this->current_object_ = obj;
  969. if (!obj->is_dynamic())
  970. {
  971. this->current_object_entry_ =
  972. new Incremental_object_entry(filename_key, obj, arg_serial, mtime);
  973. input_entry = this->current_object_entry_;
  974. if (arch != NULL)
  975. {
  976. Incremental_archive_entry* arch_entry = arch->incremental_info();
  977. gold_assert(arch_entry != NULL);
  978. arch_entry->add_object(this->current_object_entry_);
  979. }
  980. }
  981. else
  982. {
  983. this->current_object_entry_ = NULL;
  984. Stringpool::Key soname_key;
  985. Dynobj* dynobj = obj->dynobj();
  986. gold_assert(dynobj != NULL);
  987. this->strtab_->add(dynobj->soname(), false, &soname_key);
  988. input_entry = new Incremental_dynobj_entry(filename_key, soname_key, obj,
  989. arg_serial, mtime);
  990. }
  991. if (obj->is_in_system_directory())
  992. input_entry->set_is_in_system_directory();
  993. if (obj->as_needed())
  994. input_entry->set_as_needed();
  995. this->inputs_.push_back(input_entry);
  996. if (script_info != NULL)
  997. {
  998. Incremental_script_entry* script_entry = script_info->incremental_info();
  999. gold_assert(script_entry != NULL);
  1000. script_entry->add_object(input_entry);
  1001. }
  1002. }
  1003. // Record an input section SHNDX from object file OBJ.
  1004. void
  1005. Incremental_inputs::report_input_section(Object* obj, unsigned int shndx,
  1006. const char* name, off_t sh_size)
  1007. {
  1008. Stringpool::Key key = 0;
  1009. if (name != NULL)
  1010. this->strtab_->add(name, true, &key);
  1011. gold_assert(obj == this->current_object_);
  1012. gold_assert(this->current_object_entry_ != NULL);
  1013. this->current_object_entry_->add_input_section(shndx, key, sh_size);
  1014. }
  1015. // Record a kept COMDAT group belonging to object file OBJ.
  1016. void
  1017. Incremental_inputs::report_comdat_group(Object* obj, const char* name)
  1018. {
  1019. Stringpool::Key key = 0;
  1020. if (name != NULL)
  1021. this->strtab_->add(name, true, &key);
  1022. gold_assert(obj == this->current_object_);
  1023. gold_assert(this->current_object_entry_ != NULL);
  1024. this->current_object_entry_->add_comdat_group(key);
  1025. }
  1026. // Record that the input argument INPUT is a script SCRIPT. This is
  1027. // called by read_script after parsing the script and reading the list
  1028. // of inputs added by this script.
  1029. void
  1030. Incremental_inputs::report_script(Script_info* script,
  1031. unsigned int arg_serial,
  1032. Timespec mtime)
  1033. {
  1034. Stringpool::Key filename_key;
  1035. this->strtab_->add(script->filename().c_str(), false, &filename_key);
  1036. Incremental_script_entry* entry =
  1037. new Incremental_script_entry(filename_key, arg_serial, script, mtime);
  1038. this->inputs_.push_back(entry);
  1039. script->set_incremental_info(entry);
  1040. }
  1041. // Finalize the incremental link information. Called from
  1042. // Layout::finalize.
  1043. void
  1044. Incremental_inputs::finalize()
  1045. {
  1046. // Finalize the string table.
  1047. this->strtab_->set_string_offsets();
  1048. }
  1049. // Create the .gnu_incremental_inputs, _symtab, and _relocs input sections.
  1050. void
  1051. Incremental_inputs::create_data_sections(Symbol_table* symtab)
  1052. {
  1053. int reloc_align = 4;
  1054. switch (parameters->size_and_endianness())
  1055. {
  1056. #ifdef HAVE_TARGET_32_LITTLE
  1057. case Parameters::TARGET_32_LITTLE:
  1058. this->inputs_section_ =
  1059. new Output_section_incremental_inputs<32, false>(this, symtab);
  1060. reloc_align = 4;
  1061. break;
  1062. #endif
  1063. #ifdef HAVE_TARGET_32_BIG
  1064. case Parameters::TARGET_32_BIG:
  1065. this->inputs_section_ =
  1066. new Output_section_incremental_inputs<32, true>(this, symtab);
  1067. reloc_align = 4;
  1068. break;
  1069. #endif
  1070. #ifdef HAVE_TARGET_64_LITTLE
  1071. case Parameters::TARGET_64_LITTLE:
  1072. this->inputs_section_ =
  1073. new Output_section_incremental_inputs<64, false>(this, symtab);
  1074. reloc_align = 8;
  1075. break;
  1076. #endif
  1077. #ifdef HAVE_TARGET_64_BIG
  1078. case Parameters::TARGET_64_BIG:
  1079. this->inputs_section_ =
  1080. new Output_section_incremental_inputs<64, true>(this, symtab);
  1081. reloc_align = 8;
  1082. break;
  1083. #endif
  1084. default:
  1085. gold_unreachable();
  1086. }
  1087. this->symtab_section_ = new Output_data_space(4, "** incremental_symtab");
  1088. this->relocs_section_ = new Output_data_space(reloc_align,
  1089. "** incremental_relocs");
  1090. this->got_plt_section_ = new Output_data_space(4, "** incremental_got_plt");
  1091. }
  1092. // Return the sh_entsize value for the .gnu_incremental_relocs section.
  1093. unsigned int
  1094. Incremental_inputs::relocs_entsize() const
  1095. {
  1096. return 8 + 2 * parameters->target().get_size() / 8;
  1097. }
  1098. // Class Output_section_incremental_inputs.
  1099. // Finalize the offsets for each input section and supplemental info block,
  1100. // and set the final data size of the incremental output sections.
  1101. template<int size, bool big_endian>
  1102. void
  1103. Output_section_incremental_inputs<size, big_endian>::set_final_data_size()
  1104. {
  1105. const Incremental_inputs* inputs = this->inputs_;
  1106. // Offset of each input entry.
  1107. unsigned int input_offset = this->header_size;
  1108. // Offset of each supplemental info block.
  1109. unsigned int file_index = 0;
  1110. unsigned int info_offset = this->header_size;
  1111. info_offset += this->input_entry_size * inputs->input_file_count();
  1112. // Count each input file and its supplemental information block.
  1113. for (Incremental_inputs::Input_list::const_iterator p =
  1114. inputs->input_files().begin();
  1115. p != inputs->input_files().end();
  1116. ++p)
  1117. {
  1118. // Set the index and offset of the input file entry.
  1119. (*p)->set_offset(file_index, input_offset);
  1120. ++file_index;
  1121. input_offset += this->input_entry_size;
  1122. // Set the offset of the supplemental info block.
  1123. switch ((*p)->type())
  1124. {
  1125. case INCREMENTAL_INPUT_SCRIPT:
  1126. {
  1127. Incremental_script_entry *entry = (*p)->script_entry();
  1128. gold_assert(entry != NULL);
  1129. (*p)->set_info_offset(info_offset);
  1130. // Object count.
  1131. info_offset += 4;
  1132. // Each member.
  1133. info_offset += (entry->get_object_count() * 4);
  1134. }
  1135. break;
  1136. case INCREMENTAL_INPUT_OBJECT:
  1137. case INCREMENTAL_INPUT_ARCHIVE_MEMBER:
  1138. {
  1139. Incremental_object_entry* entry = (*p)->object_entry();
  1140. gold_assert(entry != NULL);
  1141. (*p)->set_info_offset(info_offset);
  1142. // Input section count, global symbol count, local symbol offset,
  1143. // local symbol count, first dynamic reloc, dynamic reloc count,
  1144. // comdat group count.
  1145. info_offset += this->object_info_size;
  1146. // Each input section.
  1147. info_offset += (entry->get_input_section_count()
  1148. * this->input_section_entry_size);
  1149. // Each global symbol.
  1150. const Object::Symbols* syms = entry->object()->get_global_symbols();
  1151. info_offset += syms->size() * this->global_sym_entry_size;
  1152. // Each comdat group.
  1153. info_offset += entry->get_comdat_group_count() * 4;
  1154. }
  1155. break;
  1156. case INCREMENTAL_INPUT_SHARED_LIBRARY:
  1157. {
  1158. Incremental_dynobj_entry* entry = (*p)->dynobj_entry();
  1159. gold_assert(entry != NULL);
  1160. (*p)->set_info_offset(info_offset);
  1161. // Global symbol count, soname index.
  1162. info_offset += 8;
  1163. // Each global symbol.
  1164. const Object::Symbols* syms = entry->object()->get_global_symbols();
  1165. gold_assert(syms != NULL);
  1166. unsigned int nsyms = syms->size();
  1167. unsigned int nsyms_out = 0;
  1168. for (unsigned int i = 0; i < nsyms; ++i)
  1169. {
  1170. const Symbol* sym = (*syms)[i];
  1171. if (sym == NULL)
  1172. continue;
  1173. if (sym->is_forwarder())
  1174. sym = this->symtab_->resolve_forwards(sym);
  1175. if (sym->symtab_index() != -1U)
  1176. ++nsyms_out;
  1177. }
  1178. info_offset += nsyms_out * 4;
  1179. }
  1180. break;
  1181. case INCREMENTAL_INPUT_ARCHIVE:
  1182. {
  1183. Incremental_archive_entry* entry = (*p)->archive_entry();
  1184. gold_assert(entry != NULL);
  1185. (*p)->set_info_offset(info_offset);
  1186. // Member count + unused global symbol count.
  1187. info_offset += 8;
  1188. // Each member.
  1189. info_offset += (entry->get_member_count() * 4);
  1190. // Each global symbol.
  1191. info_offset += (entry->get_unused_global_symbol_count() * 4);
  1192. }
  1193. break;
  1194. default:
  1195. gold_unreachable();
  1196. }
  1197. // Pad so each supplemental info block begins at an 8-byte boundary.
  1198. if (info_offset & 4)
  1199. info_offset += 4;
  1200. }
  1201. this->set_data_size(info_offset);
  1202. // Set the size of the .gnu_incremental_symtab section.
  1203. inputs->symtab_section()->set_current_data_size(this->symtab_->output_count()
  1204. * sizeof(unsigned int));
  1205. // Set the size of the .gnu_incremental_relocs section.
  1206. inputs->relocs_section()->set_current_data_size(inputs->get_reloc_count()
  1207. * this->incr_reloc_size);
  1208. // Set the size of the .gnu_incremental_got_plt section.
  1209. Sized_target<size, big_endian>* target =
  1210. parameters->sized_target<size, big_endian>();
  1211. unsigned int got_count = target->got_entry_count();
  1212. unsigned int plt_count = target->plt_entry_count();
  1213. unsigned int got_plt_size = 8; // GOT entry count, PLT entry count.
  1214. got_plt_size = (got_plt_size + got_count + 3) & ~3; // GOT type array.
  1215. got_plt_size += got_count * 8 + plt_count * 4; // GOT array, PLT array.
  1216. inputs->got_plt_section()->set_current_data_size(got_plt_size);
  1217. }
  1218. // Write the contents of the .gnu_incremental_inputs and
  1219. // .gnu_incremental_symtab sections.
  1220. template<int size, bool big_endian>
  1221. void
  1222. Output_section_incremental_inputs<size, big_endian>::do_write(Output_file* of)
  1223. {
  1224. const Incremental_inputs* inputs = this->inputs_;
  1225. Stringpool* strtab = inputs->get_stringpool();
  1226. // Get a view into the .gnu_incremental_inputs section.
  1227. const off_t off = this->offset();
  1228. const off_t oview_size = this->data_size();
  1229. unsigned char* const oview = of->get_output_view(off, oview_size);
  1230. unsigned char* pov = oview;
  1231. // Get a view into the .gnu_incremental_symtab section.
  1232. const off_t symtab_off = inputs->symtab_section()->offset();
  1233. const off_t symtab_size = inputs->symtab_section()->data_size();
  1234. unsigned char* const symtab_view = of->get_output_view(symtab_off,
  1235. symtab_size);
  1236. // Allocate an array of linked list heads for the .gnu_incremental_symtab
  1237. // section. Each element corresponds to a global symbol in the output
  1238. // symbol table, and points to the head of the linked list that threads
  1239. // through the object file input entries. The value of each element
  1240. // is the section-relative offset to a global symbol entry in a
  1241. // supplemental information block.
  1242. unsigned int global_sym_count = this->symtab_->output_count();
  1243. unsigned int* global_syms = new unsigned int[global_sym_count];
  1244. memset(global_syms, 0, global_sym_count * sizeof(unsigned int));
  1245. // Write the section header.
  1246. Stringpool::Key command_line_key = inputs->command_line_key();
  1247. pov = this->write_header(pov, inputs->input_file_count(),
  1248. strtab->get_offset_from_key(command_line_key));
  1249. // Write the list of input files.
  1250. pov = this->write_input_files(oview, pov, strtab);
  1251. // Write the supplemental information blocks for each input file.
  1252. pov = this->write_info_blocks(oview, pov, strtab, global_syms,
  1253. global_sym_count);
  1254. gold_assert(pov - oview == oview_size);
  1255. // Write the .gnu_incremental_symtab section.
  1256. gold_assert(static_cast<off_t>(global_sym_count) * 4 == symtab_size);
  1257. this->write_symtab(symtab_view, global_syms, global_sym_count);
  1258. delete[] global_syms;
  1259. // Write the .gnu_incremental_got_plt section.
  1260. const off_t got_plt_off = inputs->got_plt_section()->offset();
  1261. const off_t got_plt_size = inputs->got_plt_section()->data_size();
  1262. unsigned char* const got_plt_view = of->get_output_view(got_plt_off,
  1263. got_plt_size);
  1264. this->write_got_plt(got_plt_view, got_plt_size);
  1265. of->write_output_view(off, oview_size, oview);
  1266. of->write_output_view(symtab_off, symtab_size, symtab_view);
  1267. of->write_output_view(got_plt_off, got_plt_size, got_plt_view);
  1268. }
  1269. // Write the section header: version, input file count, offset of command line
  1270. // in the string table, and 4 bytes of padding.
  1271. template<int size, bool big_endian>
  1272. unsigned char*
  1273. Output_section_incremental_inputs<size, big_endian>::write_header(
  1274. unsigned char* pov,
  1275. unsigned int input_file_count,
  1276. section_offset_type command_line_offset)
  1277. {
  1278. Swap32::writeval(pov, INCREMENTAL_LINK_VERSION);
  1279. Swap32::writeval(pov + 4, input_file_count);
  1280. Swap32::writeval(pov + 8, command_line_offset);
  1281. Swap32::writeval(pov + 12, 0);
  1282. gold_assert(this->header_size == 16);
  1283. return pov + this->header_size;
  1284. }
  1285. // Write the input file entries.
  1286. template<int size, bool big_endian>
  1287. unsigned char*
  1288. Output_section_incremental_inputs<size, big_endian>::write_input_files(
  1289. unsigned char* oview,
  1290. unsigned char* pov,
  1291. Stringpool* strtab)
  1292. {
  1293. const Incremental_inputs* inputs = this->inputs_;
  1294. for (Incremental_inputs::Input_list::const_iterator p =
  1295. inputs->input_files().begin();
  1296. p != inputs->input_files().end();
  1297. ++p)
  1298. {
  1299. gold_assert(static_cast<unsigned int>(pov - oview) == (*p)->get_offset());
  1300. section_offset_type filename_offset =
  1301. strtab->get_offset_from_key((*p)->get_filename_key());
  1302. const Timespec& mtime = (*p)->get_mtime();
  1303. unsigned int flags = (*p)->type();
  1304. if ((*p)->is_in_system_directory())
  1305. flags |= INCREMENTAL_INPUT_IN_SYSTEM_DIR;
  1306. if ((*p)->as_needed())
  1307. flags |= INCREMENTAL_INPUT_AS_NEEDED;
  1308. Swap32::writeval(pov, filename_offset);
  1309. Swap32::writeval(pov + 4, (*p)->get_info_offset());
  1310. Swap64::writeval(pov + 8, mtime.seconds);
  1311. Swap32::writeval(pov + 16, mtime.nanoseconds);
  1312. Swap16::writeval(pov + 20, flags);
  1313. Swap16::writeval(pov + 22, (*p)->arg_serial());
  1314. gold_assert(this->input_entry_size == 24);
  1315. pov += this->input_entry_size;
  1316. }
  1317. return pov;
  1318. }
  1319. // Write the supplemental information blocks.
  1320. template<int size, bool big_endian>
  1321. unsigned char*
  1322. Output_section_incremental_inputs<size, big_endian>::write_info_blocks(
  1323. unsigned char* oview,
  1324. unsigned char* pov,
  1325. Stringpool* strtab,
  1326. unsigned int* global_syms,
  1327. unsigned int global_sym_count)
  1328. {
  1329. const Incremental_inputs* inputs = this->inputs_;
  1330. unsigned int first_global_index = this->symtab_->first_global_index();
  1331. for (Incremental_inputs::Input_list::const_iterator p =
  1332. inputs->input_files().begin();
  1333. p != inputs->input_files().end();
  1334. ++p)
  1335. {
  1336. switch ((*p)->type())
  1337. {
  1338. case INCREMENTAL_INPUT_SCRIPT:
  1339. {
  1340. gold_assert(static_cast<unsigned int>(pov - oview)
  1341. == (*p)->get_info_offset());
  1342. Incremental_script_entry* entry = (*p)->script_entry();
  1343. gold_assert(entry != NULL);
  1344. // Write the object count.
  1345. unsigned int nobjects = entry->get_object_count();
  1346. Swap32::writeval(pov, nobjects);
  1347. pov += 4;
  1348. // For each object, write the offset to its input file entry.
  1349. for (unsigned int i = 0; i < nobjects; ++i)
  1350. {
  1351. Incremental_input_entry* obj = entry->get_object(i);
  1352. Swap32::writeval(pov, obj->get_offset());
  1353. pov += 4;
  1354. }
  1355. }
  1356. break;
  1357. case INCREMENTAL_INPUT_OBJECT:
  1358. case INCREMENTAL_INPUT_ARCHIVE_MEMBER:
  1359. {
  1360. gold_assert(static_cast<unsigned int>(pov - oview)
  1361. == (*p)->get_info_offset());
  1362. Incremental_object_entry* entry = (*p)->object_entry();
  1363. gold_assert(entry != NULL);
  1364. const Object* obj = entry->object();
  1365. const Relobj* relobj = static_cast<const Relobj*>(obj);
  1366. const Object::Symbols* syms = obj->get_global_symbols();
  1367. // Write the input section count and global symbol count.
  1368. unsigned int nsections = entry->get_input_section_count();
  1369. unsigned int nsyms = syms->size();
  1370. off_t locals_offset = relobj->local_symbol_offset();
  1371. unsigned int nlocals = relobj->output_local_symbol_count();
  1372. unsigned int first_dynrel = relobj->first_dyn_reloc();
  1373. unsigned int ndynrel = relobj->dyn_reloc_count();
  1374. unsigned int ncomdat = entry->get_comdat_group_count();
  1375. Swap32::writeval(pov, nsections);
  1376. Swap32::writeval(pov + 4, nsyms);
  1377. Swap32::writeval(pov + 8, static_cast<unsigned int>(locals_offset));
  1378. Swap32::writeval(pov + 12, nlocals);
  1379. Swap32::writeval(pov + 16, first_dynrel);
  1380. Swap32::writeval(pov + 20, ndynrel);
  1381. Swap32::writeval(pov + 24, ncomdat);
  1382. Swap32::writeval(pov + 28, 0);
  1383. gold_assert(this->object_info_size == 32);
  1384. pov += this->object_info_size;
  1385. // Build a temporary array to map input section indexes
  1386. // from the original object file index to the index in the
  1387. // incremental info table.
  1388. unsigned int* index_map = new unsigned int[obj->shnum()];
  1389. memset(index_map, 0, obj->shnum() * sizeof(unsigned int));
  1390. // For each input section, write the name, output section index,
  1391. // offset within output section, and input section size.
  1392. for (unsigned int i = 0; i < nsections; i++)
  1393. {
  1394. unsigned int shndx = entry->get_input_section_index(i);
  1395. index_map[shndx] = i + 1;
  1396. Stringpool::Key key = entry->get_input_section_name_key(i);
  1397. off_t name_offset = 0;
  1398. if (key != 0)
  1399. name_offset = strtab->get_offset_from_key(key);
  1400. int out_shndx = 0;
  1401. off_t out_offset = 0;
  1402. off_t sh_size = 0;
  1403. Output_section* os = obj->output_section(shndx);
  1404. if (os != NULL)
  1405. {
  1406. out_shndx = os->out_shndx();
  1407. out_offset = obj->output_section_offset(shndx);
  1408. sh_size = entry->get_input_section_size(i);
  1409. }
  1410. Swap32::writeval(pov, name_offset);
  1411. Swap32::writeval(pov + 4, out_shndx);
  1412. Swap::writeval(pov + 8, out_offset);
  1413. Swap::writeval(pov + 8 + sizeof_addr, sh_size);
  1414. gold_assert(this->input_section_entry_size
  1415. == 8 + 2 * sizeof_addr);
  1416. pov += this->input_section_entry_size;
  1417. }
  1418. // For each global symbol, write its associated relocations,
  1419. // add it to the linked list of globals, then write the
  1420. // supplemental information: global symbol table index,
  1421. // input section index, linked list chain pointer, relocation
  1422. // count, and offset to the relocations.
  1423. for (unsigned int i = 0; i < nsyms; i++)
  1424. {
  1425. const Symbol* sym = (*syms)[i];
  1426. if (sym->is_forwarder())
  1427. sym = this->symtab_->resolve_forwards(sym);
  1428. unsigned int shndx = 0;
  1429. if (sym->source() != Symbol::FROM_OBJECT)
  1430. {
  1431. // The symbol was defined by the linker (e.g., common).
  1432. // We mark these symbols with a special SHNDX of -1,
  1433. // but exclude linker-predefined symbols and symbols
  1434. // copied from shared objects.
  1435. if (!sym->is_predefined()
  1436. && !sym->is_copied_from_dynobj())
  1437. shndx = -1U;
  1438. }
  1439. else if (sym->object() == obj && sym->is_defined())
  1440. {
  1441. bool is_ordinary;
  1442. unsigned int orig_shndx = sym->shndx(&is_ordinary);
  1443. if (is_ordinary)
  1444. shndx = index_map[orig_shndx];
  1445. else
  1446. shndx = 1;
  1447. }
  1448. unsigned int symtab_index = sym->symtab_index();
  1449. unsigned int chain = 0;
  1450. unsigned int first_reloc = 0;
  1451. unsigned int nrelocs = obj->get_incremental_reloc_count(i);
  1452. if (nrelocs > 0)
  1453. {
  1454. gold_assert(symtab_index != -1U
  1455. && (symtab_index - first_global_index
  1456. < global_sym_count));
  1457. first_reloc = obj->get_incremental_reloc_base(i);
  1458. chain = global_syms[symtab_index - first_global_index];
  1459. global_syms[symtab_index - first_global_index] =
  1460. pov - oview;
  1461. }
  1462. Swap32::writeval(pov, symtab_index);
  1463. Swap32::writeval(pov + 4, shndx);
  1464. Swap32::writeval(pov + 8, chain);
  1465. Swap32::writeval(pov + 12, nrelocs);
  1466. Swap32::writeval(pov + 16,
  1467. first_reloc * (8 + 2 * sizeof_addr));
  1468. gold_assert(this->global_sym_entry_size == 20);
  1469. pov += this->global_sym_entry_size;
  1470. }
  1471. // For each kept COMDAT group, write the group signature.
  1472. for (unsigned int i = 0; i < ncomdat; i++)
  1473. {
  1474. Stringpool::Key key = entry->get_comdat_signature_key(i);
  1475. off_t name_offset = 0;
  1476. if (key != 0)
  1477. name_offset = strtab->get_offset_from_key(key);
  1478. Swap32::writeval(pov, name_offset);
  1479. pov += 4;
  1480. }
  1481. delete[] index_map;
  1482. }
  1483. break;
  1484. case INCREMENTAL_INPUT_SHARED_LIBRARY:
  1485. {
  1486. gold_assert(static_cast<unsigned int>(pov - oview)
  1487. == (*p)->get_info_offset());
  1488. Incremental_dynobj_entry* entry = (*p)->dynobj_entry();
  1489. gold_assert(entry != NULL);
  1490. Object* obj = entry->object();
  1491. Dynobj* dynobj = obj->dynobj();
  1492. gold_assert(dynobj != NULL);
  1493. const Object::Symbols* syms = obj->get_global_symbols();
  1494. // Write the soname string table index.
  1495. section_offset_type soname_offset =
  1496. strtab->get_offset_from_key(entry->get_soname_key());
  1497. Swap32::writeval(pov, soname_offset);
  1498. pov += 4;
  1499. // Skip the global symbol count for now.
  1500. unsigned char* orig_pov = pov;
  1501. pov += 4;
  1502. // For each global symbol, write the global symbol table index.
  1503. unsigned int nsyms = syms->size();
  1504. unsigned int nsyms_out = 0;
  1505. for (unsigned int i = 0; i < nsyms; i++)
  1506. {
  1507. const Symbol* sym = (*syms)[i];
  1508. if (sym == NULL)
  1509. continue;
  1510. if (sym->is_forwarder())
  1511. sym = this->symtab_->resolve_forwards(sym);
  1512. if (sym->symtab_index() == -1U)
  1513. continue;
  1514. unsigned int flags = 0;
  1515. // If the symbol has hidden or internal visibility, we
  1516. // mark it as defined in the shared object so we don't
  1517. // try to resolve it during an incremental update.
  1518. if (sym->visibility() == elfcpp::STV_HIDDEN
  1519. || sym->visibility() == elfcpp::STV_INTERNAL)
  1520. flags = INCREMENTAL_SHLIB_SYM_DEF;
  1521. else if (sym->source() == Symbol::FROM_OBJECT
  1522. && sym->object() == obj
  1523. && sym->is_defined())
  1524. flags = INCREMENTAL_SHLIB_SYM_DEF;
  1525. else if (sym->is_copied_from_dynobj()
  1526. && this->symtab_->get_copy_source(sym) == dynobj)
  1527. flags = INCREMENTAL_SHLIB_SYM_COPY;
  1528. flags <<= INCREMENTAL_SHLIB_SYM_FLAGS_SHIFT;
  1529. Swap32::writeval(pov, sym->symtab_index() | flags);
  1530. pov += 4;
  1531. ++nsyms_out;
  1532. }
  1533. // Now write the global symbol count.
  1534. Swap32::writeval(orig_pov, nsyms_out);
  1535. }
  1536. break;
  1537. case INCREMENTAL_INPUT_ARCHIVE:
  1538. {
  1539. gold_assert(static_cast<unsigned int>(pov - oview)
  1540. == (*p)->get_info_offset());
  1541. Incremental_archive_entry* entry = (*p)->archive_entry();
  1542. gold_assert(entry != NULL);
  1543. // Write the member count and unused global symbol count.
  1544. unsigned int nmembers = entry->get_member_count();
  1545. unsigned int nsyms = entry->get_unused_global_symbol_count();
  1546. Swap32::writeval(pov, nmembers);
  1547. Swap32::writeval(pov + 4, nsyms);
  1548. pov += 8;
  1549. // For each member, write the offset to its input file entry.
  1550. for (unsigned int i = 0; i < nmembers; ++i)
  1551. {
  1552. Incremental_object_entry* member = entry->get_member(i);
  1553. Swap32::writeval(pov, member->get_offset());
  1554. pov += 4;
  1555. }
  1556. // For each global symbol, write the name offset.
  1557. for (unsigned int i = 0; i < nsyms; ++i)
  1558. {
  1559. Stringpool::Key key = entry->get_unused_global_symbol(i);
  1560. Swap32::writeval(pov, strtab->get_offset_from_key(key));
  1561. pov += 4;
  1562. }
  1563. }
  1564. break;
  1565. default:
  1566. gold_unreachable();
  1567. }
  1568. // Pad the info block to a multiple of 8 bytes.
  1569. if (static_cast<unsigned int>(pov - oview) & 4)
  1570. {
  1571. Swap32::writeval(pov, 0);
  1572. pov += 4;
  1573. }
  1574. }
  1575. return pov;
  1576. }
  1577. // Write the contents of the .gnu_incremental_symtab section.
  1578. template<int size, bool big_endian>
  1579. void
  1580. Output_section_incremental_inputs<size, big_endian>::write_symtab(
  1581. unsigned char* pov,
  1582. unsigned int* global_syms,
  1583. unsigned int global_sym_count)
  1584. {
  1585. for (unsigned int i = 0; i < global_sym_count; ++i)
  1586. {
  1587. Swap32::writeval(pov, global_syms[i]);
  1588. pov += 4;
  1589. }
  1590. }
  1591. // This struct holds the view information needed to write the
  1592. // .gnu_incremental_got_plt section.
  1593. struct Got_plt_view_info
  1594. {
  1595. // Start of the GOT type array in the output view.
  1596. unsigned char* got_type_p;
  1597. // Start of the GOT descriptor array in the output view.
  1598. unsigned char* got_desc_p;
  1599. // Start of the PLT descriptor array in the output view.
  1600. unsigned char* plt_desc_p;
  1601. // Number of GOT entries.
  1602. unsigned int got_count;
  1603. // Number of PLT entries.
  1604. unsigned int plt_count;
  1605. // Offset of the first non-reserved PLT entry (this is a target-dependent value).
  1606. unsigned int first_plt_entry_offset;
  1607. // Size of a PLT entry (this is a target-dependent value).
  1608. unsigned int plt_entry_size;
  1609. // Size of a GOT entry (this is a target-dependent value).
  1610. unsigned int got_entry_size;
  1611. // Symbol index to write in the GOT descriptor array. For global symbols,
  1612. // this is the global symbol table index; for local symbols, it is the
  1613. // local symbol table index.
  1614. unsigned int sym_index;
  1615. // Input file index to write in the GOT descriptor array. For global
  1616. // symbols, this is 0; for local symbols, it is the index of the input
  1617. // file entry in the .gnu_incremental_inputs section.
  1618. unsigned int input_index;
  1619. };
  1620. // Functor class for processing a GOT offset list for local symbols.
  1621. // Writes the GOT type and symbol index into the GOT type and descriptor
  1622. // arrays in the output section.
  1623. template<int size, bool big_endian>
  1624. class Local_got_offset_visitor : public Got_offset_list::Visitor
  1625. {
  1626. public:
  1627. Local_got_offset_visitor(struct Got_plt_view_info& info)
  1628. : info_(info)
  1629. { }
  1630. void
  1631. visit(unsigned int got_type, unsigned int got_offset, uint64_t)
  1632. {
  1633. unsigned int got_index = got_offset / this->info_.got_entry_size;
  1634. gold_assert(got_index < this->info_.got_count);
  1635. // We can only handle GOT entry types in the range 0..0x7e
  1636. // because we use a byte array to store them, and we use the
  1637. // high bit to flag a local symbol.
  1638. gold_assert(got_type < 0x7f);
  1639. this->info_.got_type_p[got_index] = got_type | 0x80;
  1640. unsigned char* pov = this->info_.got_desc_p + got_index * 8;
  1641. elfcpp::Swap<32, big_endian>::writeval(pov, this->info_.sym_index);
  1642. elfcpp::Swap<32, big_endian>::writeval(pov + 4, this->info_.input_index);
  1643. // FIXME: the uint64_t addend should be written here if powerpc64
  1644. // sym+addend got entries are to be supported, with similar changes
  1645. // to Global_got_offset_visitor and support to read them back in
  1646. // do_process_got_plt.
  1647. // FIXME: don't we need this for section symbol plus addend anyway?
  1648. // (See 2015-12-03 commit 7ef8ae7c5f35)
  1649. }
  1650. private:
  1651. struct Got_plt_view_info& info_;
  1652. };
  1653. // Functor class for processing a GOT offset list. Writes the GOT type
  1654. // and symbol index into the GOT type and descriptor arrays in the output
  1655. // section.
  1656. template<int size, bool big_endian>
  1657. class Global_got_offset_visitor : public Got_offset_list::Visitor
  1658. {
  1659. public:
  1660. Global_got_offset_visitor(struct Got_plt_view_info& info)
  1661. : info_(info)
  1662. { }
  1663. void
  1664. visit(unsigned int got_type, unsigned int got_offset, uint64_t)
  1665. {
  1666. unsigned int got_index = got_offset / this->info_.got_entry_size;
  1667. gold_assert(got_index < this->info_.got_count);
  1668. // We can only handle GOT entry types in the range 0..0x7e
  1669. // because we use a byte array to store them, and we use the
  1670. // high bit to flag a local symbol.
  1671. gold_assert(got_type < 0x7f);
  1672. this->info_.got_type_p[got_index] = got_type;
  1673. unsigned char* pov = this->info_.got_desc_p + got_index * 8;
  1674. elfcpp::Swap<32, big_endian>::writeval(pov, this->info_.sym_index);
  1675. elfcpp::Swap<32, big_endian>::writeval(pov + 4, 0);
  1676. }
  1677. private:
  1678. struct Got_plt_view_info& info_;
  1679. };
  1680. // Functor class for processing the global symbol table. Processes the
  1681. // GOT offset list for the symbol, and writes the symbol table index
  1682. // into the PLT descriptor array in the output section.
  1683. template<int size, bool big_endian>
  1684. class Global_symbol_visitor_got_plt
  1685. {
  1686. public:
  1687. Global_symbol_visitor_got_plt(struct Got_plt_view_info& info)
  1688. : info_(info)
  1689. { }
  1690. void
  1691. operator()(const Sized_symbol<size>* sym)
  1692. {
  1693. typedef Global_got_offset_visitor<size, big_endian> Got_visitor;
  1694. const Got_offset_list* got_offsets = sym->got_offset_list();
  1695. if (got_offsets != NULL)
  1696. {
  1697. this->info_.sym_index = sym->symtab_index();
  1698. this->info_.input_index = 0;
  1699. Got_visitor v(this->info_);
  1700. got_offsets->for_all_got_offsets(&v);
  1701. }
  1702. if (sym->has_plt_offset())
  1703. {
  1704. unsigned int plt_index =
  1705. ((sym->plt_offset() - this->info_.first_plt_entry_offset)
  1706. / this->info_.plt_entry_size);
  1707. gold_assert(plt_index < this->info_.plt_count);
  1708. unsigned char* pov = this->info_.plt_desc_p + plt_index * 4;
  1709. elfcpp::Swap<32, big_endian>::writeval(pov, sym->symtab_index());
  1710. }
  1711. }
  1712. private:
  1713. struct Got_plt_view_info& info_;
  1714. };
  1715. // Write the contents of the .gnu_incremental_got_plt section.
  1716. template<int size, bool big_endian>
  1717. void
  1718. Output_section_incremental_inputs<size, big_endian>::write_got_plt(
  1719. unsigned char* pov,
  1720. off_t view_size)
  1721. {
  1722. Sized_target<size, big_endian>* target =
  1723. parameters->sized_target<size, big_endian>();
  1724. // Set up the view information for the functors.
  1725. struct Got_plt_view_info view_info;
  1726. view_info.got_count = target->got_entry_count();
  1727. view_info.plt_count = target->plt_entry_count();
  1728. view_info.first_plt_entry_offset = target->first_plt_entry_offset();
  1729. view_info.plt_entry_size = target->plt_entry_size();
  1730. view_info.got_entry_size = target->got_entry_size();
  1731. view_info.got_type_p = pov + 8;
  1732. view_info.got_desc_p = (view_info.got_type_p
  1733. + ((view_info.got_count + 3) & ~3));
  1734. view_info.plt_desc_p = view_info.got_desc_p + view_info.got_count * 8;
  1735. gold_assert(pov + view_size ==
  1736. view_info.plt_desc_p + view_info.plt_count * 4);
  1737. // Write the section header.
  1738. Swap32::writeval(pov, view_info.got_count);
  1739. Swap32::writeval(pov + 4, view_info.plt_count);
  1740. // Initialize the GOT type array to 0xff (reserved).
  1741. memset(view_info.got_type_p, 0xff, view_info.got_count);
  1742. // Write the incremental GOT descriptors for local symbols.
  1743. typedef Local_got_offset_visitor<size, big_endian> Got_visitor;
  1744. for (Incremental_inputs::Input_list::const_iterator p =
  1745. this->inputs_->input_files().begin();
  1746. p != this->inputs_->input_files().end();
  1747. ++p)
  1748. {
  1749. if ((*p)->type() != INCREMENTAL_INPUT_OBJECT
  1750. && (*p)->type() != INCREMENTAL_INPUT_ARCHIVE_MEMBER)
  1751. continue;
  1752. Incremental_object_entry* entry = (*p)->object_entry();
  1753. gold_assert(entry != NULL);
  1754. const Object* obj = entry->object();
  1755. gold_assert(obj != NULL);
  1756. view_info.input_index = (*p)->get_file_index();
  1757. Got_visitor v(view_info);
  1758. obj->for_all_local_got_entries(&v);
  1759. }
  1760. // Write the incremental GOT and PLT descriptors for global symbols.
  1761. typedef Global_symbol_visitor_got_plt<size, big_endian> Symbol_visitor;
  1762. symtab_->for_all_symbols<size, Symbol_visitor>(Symbol_visitor(view_info));
  1763. }
  1764. // Class Sized_relobj_incr. Most of these methods are not used for
  1765. // Incremental objects, but are required to be implemented by the
  1766. // base class Object.
  1767. template<int size, bool big_endian>
  1768. Sized_relobj_incr<size, big_endian>::Sized_relobj_incr(
  1769. const std::string& name,
  1770. Sized_incremental_binary<size, big_endian>* ibase,
  1771. unsigned int input_file_index)
  1772. : Sized_relobj<size, big_endian>(name, NULL), ibase_(ibase),
  1773. input_file_index_(input_file_index),
  1774. input_reader_(ibase->inputs_reader().input_file(input_file_index)),
  1775. local_symbol_count_(0), output_local_dynsym_count_(0),
  1776. local_symbol_index_(0), local_symbol_offset_(0), local_dynsym_offset_(0),
  1777. symbols_(), defined_count_(0), incr_reloc_offset_(-1U),
  1778. incr_reloc_count_(0), incr_reloc_output_index_(0), incr_relocs_(NULL),
  1779. local_symbols_()
  1780. {
  1781. if (this->input_reader_.is_in_system_directory())
  1782. this->set_is_in_system_directory();
  1783. const unsigned int shnum = this->input_reader_.get_input_section_count() + 1;
  1784. this->set_shnum(shnum);
  1785. ibase->set_input_object(input_file_index, this);
  1786. }
  1787. // Read the symbols.
  1788. template<int size, bool big_endian>
  1789. void
  1790. Sized_relobj_incr<size, big_endian>::do_read_symbols(Read_symbols_data*)
  1791. {
  1792. gold_unreachable();
  1793. }
  1794. // Lay out the input sections.
  1795. template<int size, bool big_endian>
  1796. void
  1797. Sized_relobj_incr<size, big_endian>::do_layout(
  1798. Symbol_table*,
  1799. Layout* layout,
  1800. Read_symbols_data*)
  1801. {
  1802. const unsigned int shnum = this->shnum();
  1803. Incremental_inputs* incremental_inputs = layout->incremental_inputs();
  1804. gold_assert(incremental_inputs != NULL);
  1805. Output_sections& out_sections(this->output_sections());
  1806. out_sections.resize(shnum);
  1807. this->section_offsets().resize(shnum);
  1808. // Keep track of .debug_info and .debug_types sections.
  1809. std::vector<unsigned int> debug_info_sections;
  1810. std::vector<unsigned int> debug_types_sections;
  1811. for (unsigned int i = 1; i < shnum; i++)
  1812. {
  1813. typename Input_entry_reader::Input_section_info sect =
  1814. this->input_reader_.get_input_section(i - 1);
  1815. // Add the section to the incremental inputs layout.
  1816. incremental_inputs->report_input_section(this, i, sect.name,
  1817. sect.sh_size);
  1818. if (sect.output_shndx == 0 || sect.sh_offset == -1)
  1819. continue;
  1820. Output_section* os = this->ibase_->output_section(sect.output_shndx);
  1821. gold_assert(os != NULL);
  1822. out_sections[i] = os;
  1823. this->section_offsets()[i] = static_cast<Address>(sect.sh_offset);
  1824. // When generating a .gdb_index section, we do additional
  1825. // processing of .debug_info and .debug_types sections after all
  1826. // the other sections.
  1827. if (parameters->options().gdb_index())
  1828. {
  1829. const char* name = os->name();
  1830. if (strcmp(name, ".debug_info") == 0)
  1831. debug_info_sections.push_back(i);
  1832. else if (strcmp(name, ".debug_types") == 0)
  1833. debug_types_sections.push_back(i);
  1834. }
  1835. }
  1836. // Process the COMDAT groups.
  1837. unsigned int ncomdat = this->input_reader_.get_comdat_group_count();
  1838. for (unsigned int i = 0; i < ncomdat; i++)
  1839. {
  1840. const char* signature = this->input_reader_.get_comdat_group_signature(i);
  1841. if (signature == NULL || signature[0] == '\0')
  1842. this->error(_("COMDAT group has no signature"));
  1843. bool keep = layout->find_or_add_kept_section(signature, this, i, true,
  1844. true, NULL);
  1845. if (keep)
  1846. incremental_inputs->report_comdat_group(this, signature);
  1847. else
  1848. this->error(_("COMDAT group %s included twice in incremental link"),
  1849. signature);
  1850. }
  1851. // When building a .gdb_index section, scan the .debug_info and
  1852. // .debug_types sections.
  1853. for (std::vector<unsigned int>::const_iterator p
  1854. = debug_info_sections.begin();
  1855. p != debug_info_sections.end();
  1856. ++p)
  1857. {
  1858. unsigned int i = *p;
  1859. layout->add_to_gdb_index(false, this, NULL, 0, i, 0, 0);
  1860. }
  1861. for (std::vector<unsigned int>::const_iterator p
  1862. = debug_types_sections.begin();
  1863. p != debug_types_sections.end();
  1864. ++p)
  1865. {
  1866. unsigned int i = *p;
  1867. layout->add_to_gdb_index(true, this, 0, 0, i, 0, 0);
  1868. }
  1869. }
  1870. // Layout sections whose layout was deferred while waiting for
  1871. // input files from a plugin.
  1872. template<int size, bool big_endian>
  1873. void
  1874. Sized_relobj_incr<size, big_endian>::do_layout_deferred_sections(Layout*)
  1875. {
  1876. }
  1877. // Add the symbols to the symbol table.
  1878. template<int size, bool big_endian>
  1879. void
  1880. Sized_relobj_incr<size, big_endian>::do_add_symbols(
  1881. Symbol_table* symtab,
  1882. Read_symbols_data*,
  1883. Layout*)
  1884. {
  1885. const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
  1886. unsigned char symbuf[sym_size];
  1887. elfcpp::Sym_write<size, big_endian> osym(symbuf);
  1888. typedef typename elfcpp::Elf_types<size>::Elf_WXword Elf_size_type;
  1889. unsigned int nsyms = this->input_reader_.get_global_symbol_count();
  1890. this->symbols_.resize(nsyms);
  1891. Incremental_binary::View symtab_view(NULL);
  1892. unsigned int symtab_count;
  1893. elfcpp::Elf_strtab strtab(NULL, 0);
  1894. this->ibase_->get_symtab_view(&symtab_view, &symtab_count, &strtab);
  1895. Incremental_symtab_reader<big_endian> isymtab(this->ibase_->symtab_reader());
  1896. unsigned int isym_count = isymtab.symbol_count();
  1897. unsigned int first_global = symtab_count - isym_count;
  1898. const unsigned char* sym_p;
  1899. for (unsigned int i = 0; i < nsyms; ++i)
  1900. {
  1901. Incremental_global_symbol_reader<big_endian> info =
  1902. this->input_reader_.get_global_symbol_reader(i);
  1903. unsigned int output_symndx = info.output_symndx();
  1904. sym_p = symtab_view.data() + output_symndx * sym_size;
  1905. elfcpp::Sym<size, big_endian> gsym(sym_p);
  1906. const char* name;
  1907. if (!strtab.get_c_string(gsym.get_st_name(), &name))
  1908. name = "";
  1909. typename elfcpp::Elf_types<size>::Elf_Addr v = gsym.get_st_value();
  1910. unsigned int shndx = gsym.get_st_shndx();
  1911. elfcpp::STB st_bind = gsym.get_st_bind();
  1912. elfcpp::STT st_type = gsym.get_st_type();
  1913. // Local hidden symbols start out as globals, but get converted to
  1914. // to local during output.
  1915. if (st_bind == elfcpp::STB_LOCAL)
  1916. st_bind = elfcpp::STB_GLOBAL;
  1917. unsigned int input_shndx = info.shndx();
  1918. if (input_shndx == 0 || input_shndx == -1U)
  1919. {
  1920. shndx = elfcpp::SHN_UNDEF;
  1921. v = 0;
  1922. }
  1923. else if (shndx != elfcpp::SHN_ABS)
  1924. {
  1925. // Find the input section and calculate the section-relative value.
  1926. gold_assert(shndx != elfcpp::SHN_UNDEF);
  1927. Output_section* os = this->ibase_->output_section(shndx);
  1928. gold_assert(os != NULL && os->has_fixed_layout());
  1929. typename Input_entry_reader::Input_section_info sect =
  1930. this->input_reader_.get_input_section(input_shndx - 1);
  1931. gold_assert(sect.output_shndx == shndx);
  1932. if (st_type != elfcpp::STT_TLS)
  1933. v -= os->address();
  1934. v -= sect.sh_offset;
  1935. shndx = input_shndx;
  1936. }
  1937. osym.put_st_name(0);
  1938. osym.put_st_value(v);
  1939. osym.put_st_size(gsym.get_st_size());
  1940. osym.put_st_info(st_bind, st_type);
  1941. osym.put_st_other(gsym.get_st_other());
  1942. osym.put_st_shndx(shndx);
  1943. elfcpp::Sym<size, big_endian> sym(symbuf);
  1944. Symbol* res = symtab->add_from_incrobj(this, name, NULL, &sym);
  1945. if (shndx != elfcpp::SHN_UNDEF)
  1946. ++this->defined_count_;
  1947. // If this is a linker-defined symbol that hasn't yet been defined,
  1948. // define it now.
  1949. if (input_shndx == -1U && !res->is_defined())
  1950. {
  1951. shndx = gsym.get_st_shndx();
  1952. v = gsym.get_st_value();
  1953. Elf_size_type symsize = gsym.get_st_size();
  1954. if (shndx == elfcpp::SHN_ABS)
  1955. {
  1956. symtab->define_as_constant(name, NULL,
  1957. Symbol_table::INCREMENTAL_BASE,
  1958. v, symsize, st_type, st_bind,
  1959. gsym.get_st_visibility(), 0,
  1960. false, false);
  1961. }
  1962. else
  1963. {
  1964. Output_section* os = this->ibase_->output_section(shndx);
  1965. gold_assert(os != NULL && os->has_fixed_layout());
  1966. v -= os->address();
  1967. if (symsize > 0)
  1968. os->reserve(v, symsize);
  1969. symtab->define_in_output_data(name, NULL,
  1970. Symbol_table::INCREMENTAL_BASE,
  1971. os, v, symsize, st_type, st_bind,
  1972. gsym.get_st_visibility(), 0,
  1973. false, false);
  1974. }
  1975. }
  1976. this->symbols_[i] = res;
  1977. this->ibase_->add_global_symbol(output_symndx - first_global, res);
  1978. }
  1979. }
  1980. // Return TRUE if we should include this object from an archive library.
  1981. template<int size, bool big_endian>
  1982. Archive::Should_include
  1983. Sized_relobj_incr<size, big_endian>::do_should_include_member(
  1984. Symbol_table*,
  1985. Layout*,
  1986. Read_symbols_data*,
  1987. std::string*)
  1988. {
  1989. gold_unreachable();
  1990. }
  1991. // Iterate over global symbols, calling a visitor class V for each.
  1992. template<int size, bool big_endian>
  1993. void
  1994. Sized_relobj_incr<size, big_endian>::do_for_all_global_symbols(
  1995. Read_symbols_data*,
  1996. Library_base::Symbol_visitor_base*)
  1997. {
  1998. // This routine is not used for incremental objects.
  1999. }
  2000. // Get the size of a section.
  2001. template<int size, bool big_endian>
  2002. uint64_t
  2003. Sized_relobj_incr<size, big_endian>::do_section_size(unsigned int)
  2004. {
  2005. gold_unreachable();
  2006. }
  2007. // Get the name of a section. This returns the name of the output
  2008. // section, because we don't usually track the names of the input
  2009. // sections.
  2010. template<int size, bool big_endian>
  2011. std::string
  2012. Sized_relobj_incr<size, big_endian>::do_section_name(unsigned int shndx) const
  2013. {
  2014. const Output_sections& out_sections(this->output_sections());
  2015. const Output_section* os = out_sections[shndx];
  2016. if (os == NULL)
  2017. return std::string();
  2018. return os->name();
  2019. }
  2020. // Return a view of the contents of a section.
  2021. template<int size, bool big_endian>
  2022. const unsigned char*
  2023. Sized_relobj_incr<size, big_endian>::do_section_contents(
  2024. unsigned int shndx,
  2025. section_size_type* plen,
  2026. bool)
  2027. {
  2028. Output_sections& out_sections(this->output_sections());
  2029. Output_section* os = out_sections[shndx];
  2030. gold_assert(os != NULL);
  2031. off_t section_offset = os->offset();
  2032. typename Input_entry_reader::Input_section_info sect =
  2033. this->input_reader_.get_input_section(shndx - 1);
  2034. section_offset += sect.sh_offset;
  2035. *plen = sect.sh_size;
  2036. return this->ibase_->view(section_offset, sect.sh_size).data();
  2037. }
  2038. // Return section flags.
  2039. template<int size, bool big_endian>
  2040. uint64_t
  2041. Sized_relobj_incr<size, big_endian>::do_section_flags(unsigned int)
  2042. {
  2043. gold_unreachable();
  2044. }
  2045. // Return section entsize.
  2046. template<int size, bool big_endian>
  2047. uint64_t
  2048. Sized_relobj_incr<size, big_endian>::do_section_entsize(unsigned int)
  2049. {
  2050. gold_unreachable();
  2051. }
  2052. // Return section address.
  2053. template<int size, bool big_endian>
  2054. uint64_t
  2055. Sized_relobj_incr<size, big_endian>::do_section_address(unsigned int)
  2056. {
  2057. gold_unreachable();
  2058. }
  2059. // Return section type.
  2060. template<int size, bool big_endian>
  2061. unsigned int
  2062. Sized_relobj_incr<size, big_endian>::do_section_type(unsigned int)
  2063. {
  2064. gold_unreachable();
  2065. }
  2066. // Return the section link field.
  2067. template<int size, bool big_endian>
  2068. unsigned int
  2069. Sized_relobj_incr<size, big_endian>::do_section_link(unsigned int)
  2070. {
  2071. gold_unreachable();
  2072. }
  2073. // Return the section link field.
  2074. template<int size, bool big_endian>
  2075. unsigned int
  2076. Sized_relobj_incr<size, big_endian>::do_section_info(unsigned int)
  2077. {
  2078. gold_unreachable();
  2079. }
  2080. // Return the section alignment.
  2081. template<int size, bool big_endian>
  2082. uint64_t
  2083. Sized_relobj_incr<size, big_endian>::do_section_addralign(unsigned int)
  2084. {
  2085. gold_unreachable();
  2086. }
  2087. // Return the Xindex structure to use.
  2088. template<int size, bool big_endian>
  2089. Xindex*
  2090. Sized_relobj_incr<size, big_endian>::do_initialize_xindex()
  2091. {
  2092. gold_unreachable();
  2093. }
  2094. // Get symbol counts.
  2095. template<int size, bool big_endian>
  2096. void
  2097. Sized_relobj_incr<size, big_endian>::do_get_global_symbol_counts(
  2098. const Symbol_table*,
  2099. size_t* defined,
  2100. size_t* used) const
  2101. {
  2102. *defined = this->defined_count_;
  2103. size_t count = 0;
  2104. for (typename Symbols::const_iterator p = this->symbols_.begin();
  2105. p != this->symbols_.end();
  2106. ++p)
  2107. if (*p != NULL
  2108. && (*p)->source() == Symbol::FROM_OBJECT
  2109. && (*p)->object() == this
  2110. && (*p)->is_defined())
  2111. ++count;
  2112. *used = count;
  2113. }
  2114. // Read the relocs.
  2115. template<int size, bool big_endian>
  2116. void
  2117. Sized_relobj_incr<size, big_endian>::do_read_relocs(Read_relocs_data*)
  2118. {
  2119. }
  2120. // Process the relocs to find list of referenced sections. Used only
  2121. // during garbage collection.
  2122. template<int size, bool big_endian>
  2123. void
  2124. Sized_relobj_incr<size, big_endian>::do_gc_process_relocs(Symbol_table*,
  2125. Layout*,
  2126. Read_relocs_data*)
  2127. {
  2128. gold_unreachable();
  2129. }
  2130. // Scan the relocs and adjust the symbol table.
  2131. template<int size, bool big_endian>
  2132. void
  2133. Sized_relobj_incr<size, big_endian>::do_scan_relocs(Symbol_table*,
  2134. Layout* layout,
  2135. Read_relocs_data*)
  2136. {
  2137. // Count the incremental relocations for this object.
  2138. unsigned int nsyms = this->input_reader_.get_global_symbol_count();
  2139. this->allocate_incremental_reloc_counts();
  2140. for (unsigned int i = 0; i < nsyms; i++)
  2141. {
  2142. Incremental_global_symbol_reader<big_endian> sym =
  2143. this->input_reader_.get_global_symbol_reader(i);
  2144. unsigned int reloc_count = sym.reloc_count();
  2145. if (reloc_count > 0 && this->incr_reloc_offset_ == -1U)
  2146. this->incr_reloc_offset_ = sym.reloc_offset();
  2147. this->incr_reloc_count_ += reloc_count;
  2148. for (unsigned int j = 0; j < reloc_count; j++)
  2149. this->count_incremental_reloc(i);
  2150. }
  2151. this->incr_reloc_output_index_ =
  2152. layout->incremental_inputs()->get_reloc_count();
  2153. this->finalize_incremental_relocs(layout, false);
  2154. // The incoming incremental relocations may not end up in the same
  2155. // location after the incremental update, because the incremental info
  2156. // is regenerated in each link. Because the new location may overlap
  2157. // with other data in the updated output file, we need to copy the
  2158. // relocations into a buffer so that we can still read them safely
  2159. // after we start writing updates to the output file.
  2160. if (this->incr_reloc_count_ > 0)
  2161. {
  2162. const Incremental_relocs_reader<size, big_endian>& relocs_reader =
  2163. this->ibase_->relocs_reader();
  2164. const unsigned int incr_reloc_size = relocs_reader.reloc_size;
  2165. unsigned int len = this->incr_reloc_count_ * incr_reloc_size;
  2166. this->incr_relocs_ = new unsigned char[len];
  2167. memcpy(this->incr_relocs_,
  2168. relocs_reader.data(this->incr_reloc_offset_),
  2169. len);
  2170. }
  2171. }
  2172. // Count the local symbols.
  2173. template<int size, bool big_endian>
  2174. void
  2175. Sized_relobj_incr<size, big_endian>::do_count_local_symbols(
  2176. Stringpool_template<char>* pool,
  2177. Stringpool_template<char>*)
  2178. {
  2179. const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
  2180. // Set the count of local symbols based on the incremental info.
  2181. unsigned int nlocals = this->input_reader_.get_local_symbol_count();
  2182. this->local_symbol_count_ = nlocals;
  2183. this->local_symbols_.reserve(nlocals);
  2184. // Get views of the base file's symbol table and string table.
  2185. Incremental_binary::View symtab_view(NULL);
  2186. unsigned int symtab_count;
  2187. elfcpp::Elf_strtab strtab(NULL, 0);
  2188. this->ibase_->get_symtab_view(&symtab_view, &symtab_count, &strtab);
  2189. // Read the local symbols from the base file's symbol table.
  2190. off_t off = this->input_reader_.get_local_symbol_offset();
  2191. const unsigned char* symp = symtab_view.data() + off;
  2192. for (unsigned int i = 0; i < nlocals; ++i, symp += sym_size)
  2193. {
  2194. elfcpp::Sym<size, big_endian> sym(symp);
  2195. const char* name;
  2196. if (!strtab.get_c_string(sym.get_st_name(), &name))
  2197. name = "";
  2198. gold_debug(DEBUG_INCREMENTAL, "Local symbol %d: %s", i, name);
  2199. name = pool->add(name, true, NULL);
  2200. this->local_symbols_.push_back(Local_symbol(name,
  2201. sym.get_st_value(),
  2202. sym.get_st_size(),
  2203. sym.get_st_shndx(),
  2204. sym.get_st_type(),
  2205. false));
  2206. }
  2207. }
  2208. // Finalize the local symbols.
  2209. template<int size, bool big_endian>
  2210. unsigned int
  2211. Sized_relobj_incr<size, big_endian>::do_finalize_local_symbols(
  2212. unsigned int index,
  2213. off_t off,
  2214. Symbol_table*)
  2215. {
  2216. this->local_symbol_index_ = index;
  2217. this->local_symbol_offset_ = off;
  2218. return index + this->local_symbol_count_;
  2219. }
  2220. // Set the offset where local dynamic symbol information will be stored.
  2221. template<int size, bool big_endian>
  2222. unsigned int
  2223. Sized_relobj_incr<size, big_endian>::do_set_local_dynsym_indexes(
  2224. unsigned int index)
  2225. {
  2226. // FIXME: set local dynsym indexes.
  2227. return index;
  2228. }
  2229. // Set the offset where local dynamic symbol information will be stored.
  2230. template<int size, bool big_endian>
  2231. unsigned int
  2232. Sized_relobj_incr<size, big_endian>::do_set_local_dynsym_offset(off_t)
  2233. {
  2234. return 0;
  2235. }
  2236. // Relocate the input sections and write out the local symbols.
  2237. // We don't actually do any relocation here. For unchanged input files,
  2238. // we reapply relocations only for symbols that have changed; that happens
  2239. // in Layout_task_runner::run(). We do need to rewrite the incremental
  2240. // relocations for this object.
  2241. template<int size, bool big_endian>
  2242. void
  2243. Sized_relobj_incr<size, big_endian>::do_relocate(const Symbol_table*,
  2244. const Layout* layout,
  2245. Output_file* of)
  2246. {
  2247. if (this->incr_reloc_count_ == 0)
  2248. return;
  2249. const unsigned int incr_reloc_size =
  2250. Incremental_relocs_reader<size, big_endian>::reloc_size;
  2251. // Get a view for the .gnu_incremental_relocs section.
  2252. Incremental_inputs* inputs = layout->incremental_inputs();
  2253. gold_assert(inputs != NULL);
  2254. const off_t relocs_off = inputs->relocs_section()->offset();
  2255. const off_t relocs_size = inputs->relocs_section()->data_size();
  2256. unsigned char* const view = of->get_output_view(relocs_off, relocs_size);
  2257. // Copy the relocations from the buffer.
  2258. off_t off = this->incr_reloc_output_index_ * incr_reloc_size;
  2259. unsigned int len = this->incr_reloc_count_ * incr_reloc_size;
  2260. memcpy(view + off, this->incr_relocs_, len);
  2261. // The output section table may have changed, so we need to map
  2262. // the old section index to the new section index for each relocation.
  2263. for (unsigned int i = 0; i < this->incr_reloc_count_; ++i)
  2264. {
  2265. unsigned char* pov = view + off + i * incr_reloc_size;
  2266. unsigned int shndx = elfcpp::Swap<32, big_endian>::readval(pov + 4);
  2267. Output_section* os = this->ibase_->output_section(shndx);
  2268. gold_assert(os != NULL);
  2269. shndx = os->out_shndx();
  2270. elfcpp::Swap<32, big_endian>::writeval(pov + 4, shndx);
  2271. }
  2272. of->write_output_view(off, len, view);
  2273. // Get views into the output file for the portions of the symbol table
  2274. // and the dynamic symbol table that we will be writing.
  2275. off_t symtab_off = layout->symtab_section()->offset();
  2276. off_t output_size = this->local_symbol_count_ * This::sym_size;
  2277. unsigned char* oview = NULL;
  2278. if (output_size > 0)
  2279. oview = of->get_output_view(symtab_off + this->local_symbol_offset_,
  2280. output_size);
  2281. off_t dyn_output_size = this->output_local_dynsym_count_ * sym_size;
  2282. unsigned char* dyn_oview = NULL;
  2283. if (dyn_output_size > 0)
  2284. dyn_oview = of->get_output_view(this->local_dynsym_offset_,
  2285. dyn_output_size);
  2286. // Write the local symbols.
  2287. unsigned char* ov = oview;
  2288. unsigned char* dyn_ov = dyn_oview;
  2289. const Stringpool* sympool = layout->sympool();
  2290. const Stringpool* dynpool = layout->dynpool();
  2291. Output_symtab_xindex* symtab_xindex = layout->symtab_xindex();
  2292. Output_symtab_xindex* dynsym_xindex = layout->dynsym_xindex();
  2293. for (unsigned int i = 0; i < this->local_symbol_count_; ++i)
  2294. {
  2295. Local_symbol& lsym(this->local_symbols_[i]);
  2296. bool is_ordinary;
  2297. unsigned int st_shndx = this->adjust_sym_shndx(i, lsym.st_shndx,
  2298. &is_ordinary);
  2299. if (is_ordinary)
  2300. {
  2301. Output_section* os = this->ibase_->output_section(st_shndx);
  2302. st_shndx = os->out_shndx();
  2303. if (st_shndx >= elfcpp::SHN_LORESERVE)
  2304. {
  2305. symtab_xindex->add(this->local_symbol_index_ + i, st_shndx);
  2306. if (lsym.needs_dynsym_entry)
  2307. dynsym_xindex->add(lsym.output_dynsym_index, st_shndx);
  2308. st_shndx = elfcpp::SHN_XINDEX;
  2309. }
  2310. }
  2311. // Write the symbol to the output symbol table.
  2312. {
  2313. elfcpp::Sym_write<size, big_endian> osym(ov);
  2314. osym.put_st_name(sympool->get_offset(lsym.name));
  2315. osym.put_st_value(lsym.st_value);
  2316. osym.put_st_size(lsym.st_size);
  2317. osym.put_st_info(elfcpp::STB_LOCAL,
  2318. static_cast<elfcpp::STT>(lsym.st_type));
  2319. osym.put_st_other(0);
  2320. osym.put_st_shndx(st_shndx);
  2321. ov += sym_size;
  2322. }
  2323. // Write the symbol to the output dynamic symbol table.
  2324. if (lsym.needs_dynsym_entry)
  2325. {
  2326. gold_assert(dyn_ov < dyn_oview + dyn_output_size);
  2327. elfcpp::Sym_write<size, big_endian> osym(dyn_ov);
  2328. osym.put_st_name(dynpool->get_offset(lsym.name));
  2329. osym.put_st_value(lsym.st_value);
  2330. osym.put_st_size(lsym.st_size);
  2331. osym.put_st_info(elfcpp::STB_LOCAL,
  2332. static_cast<elfcpp::STT>(lsym.st_type));
  2333. osym.put_st_other(0);
  2334. osym.put_st_shndx(st_shndx);
  2335. dyn_ov += sym_size;
  2336. }
  2337. }
  2338. if (output_size > 0)
  2339. {
  2340. gold_assert(ov - oview == output_size);
  2341. of->write_output_view(symtab_off + this->local_symbol_offset_,
  2342. output_size, oview);
  2343. }
  2344. if (dyn_output_size > 0)
  2345. {
  2346. gold_assert(dyn_ov - dyn_oview == dyn_output_size);
  2347. of->write_output_view(this->local_dynsym_offset_, dyn_output_size,
  2348. dyn_oview);
  2349. }
  2350. }
  2351. // Set the offset of a section.
  2352. template<int size, bool big_endian>
  2353. void
  2354. Sized_relobj_incr<size, big_endian>::do_set_section_offset(unsigned int,
  2355. uint64_t)
  2356. {
  2357. }
  2358. // Class Sized_incr_dynobj. Most of these methods are not used for
  2359. // Incremental objects, but are required to be implemented by the
  2360. // base class Object.
  2361. template<int size, bool big_endian>
  2362. Sized_incr_dynobj<size, big_endian>::Sized_incr_dynobj(
  2363. const std::string& name,
  2364. Sized_incremental_binary<size, big_endian>* ibase,
  2365. unsigned int input_file_index)
  2366. : Dynobj(name, NULL), ibase_(ibase),
  2367. input_file_index_(input_file_index),
  2368. input_reader_(ibase->inputs_reader().input_file(input_file_index)),
  2369. symbols_(), defined_count_(0)
  2370. {
  2371. if (this->input_reader_.is_in_system_directory())
  2372. this->set_is_in_system_directory();
  2373. if (this->input_reader_.as_needed())
  2374. this->set_as_needed();
  2375. this->set_soname_string(this->input_reader_.get_soname());
  2376. this->set_shnum(0);
  2377. }
  2378. // Read the symbols.
  2379. template<int size, bool big_endian>
  2380. void
  2381. Sized_incr_dynobj<size, big_endian>::do_read_symbols(Read_symbols_data*)
  2382. {
  2383. gold_unreachable();
  2384. }
  2385. // Lay out the input sections.
  2386. template<int size, bool big_endian>
  2387. void
  2388. Sized_incr_dynobj<size, big_endian>::do_layout(
  2389. Symbol_table*,
  2390. Layout*,
  2391. Read_symbols_data*)
  2392. {
  2393. }
  2394. // Add the symbols to the symbol table.
  2395. template<int size, bool big_endian>
  2396. void
  2397. Sized_incr_dynobj<size, big_endian>::do_add_symbols(
  2398. Symbol_table* symtab,
  2399. Read_symbols_data*,
  2400. Layout*)
  2401. {
  2402. const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
  2403. unsigned char symbuf[sym_size];
  2404. elfcpp::Sym_write<size, big_endian> osym(symbuf);
  2405. unsigned int nsyms = this->input_reader_.get_global_symbol_count();
  2406. this->symbols_.resize(nsyms);
  2407. Incremental_binary::View symtab_view(NULL);
  2408. unsigned int symtab_count;
  2409. elfcpp::Elf_strtab strtab(NULL, 0);
  2410. this->ibase_->get_symtab_view(&symtab_view, &symtab_count, &strtab);
  2411. Incremental_symtab_reader<big_endian> isymtab(this->ibase_->symtab_reader());
  2412. unsigned int isym_count = isymtab.symbol_count();
  2413. unsigned int first_global = symtab_count - isym_count;
  2414. // We keep a set of symbols that we have generated COPY relocations
  2415. // for, indexed by the symbol value. We do not need more than one
  2416. // COPY relocation per address.
  2417. typedef typename std::set<Address> Copied_symbols;
  2418. Copied_symbols copied_symbols;
  2419. const unsigned char* sym_p;
  2420. for (unsigned int i = 0; i < nsyms; ++i)
  2421. {
  2422. bool is_def;
  2423. bool is_copy;
  2424. unsigned int output_symndx =
  2425. this->input_reader_.get_output_symbol_index(i, &is_def, &is_copy);
  2426. sym_p = symtab_view.data() + output_symndx * sym_size;
  2427. elfcpp::Sym<size, big_endian> gsym(sym_p);
  2428. const char* name;
  2429. if (!strtab.get_c_string(gsym.get_st_name(), &name))
  2430. name = "";
  2431. Address v;
  2432. unsigned int shndx;
  2433. elfcpp::STB st_bind = gsym.get_st_bind();
  2434. elfcpp::STT st_type = gsym.get_st_type();
  2435. // Local hidden symbols start out as globals, but get converted to
  2436. // to local during output.
  2437. if (st_bind == elfcpp::STB_LOCAL)
  2438. st_bind = elfcpp::STB_GLOBAL;
  2439. if (!is_def)
  2440. {
  2441. shndx = elfcpp::SHN_UNDEF;
  2442. v = 0;
  2443. }
  2444. else
  2445. {
  2446. // For a symbol defined in a shared object, the section index
  2447. // is meaningless, as long as it's not SHN_UNDEF.
  2448. shndx = 1;
  2449. v = gsym.get_st_value();
  2450. ++this->defined_count_;
  2451. }
  2452. osym.put_st_name(0);
  2453. osym.put_st_value(v);
  2454. osym.put_st_size(gsym.get_st_size());
  2455. osym.put_st_info(st_bind, st_type);
  2456. osym.put_st_other(gsym.get_st_other());
  2457. osym.put_st_shndx(shndx);
  2458. elfcpp::Sym<size, big_endian> sym(symbuf);
  2459. Sized_symbol<size>* res =
  2460. symtab->add_from_incrobj<size, big_endian>(this, name, NULL, &sym);
  2461. this->symbols_[i] = res;
  2462. this->ibase_->add_global_symbol(output_symndx - first_global,
  2463. this->symbols_[i]);
  2464. if (is_copy)
  2465. {
  2466. std::pair<typename Copied_symbols::iterator, bool> ins =
  2467. copied_symbols.insert(v);
  2468. if (ins.second)
  2469. {
  2470. unsigned int shndx = gsym.get_st_shndx();
  2471. Output_section* os = this->ibase_->output_section(shndx);
  2472. off_t offset = v - os->address();
  2473. this->ibase_->add_copy_reloc(this->symbols_[i], os, offset);
  2474. }
  2475. }
  2476. }
  2477. }
  2478. // Return TRUE if we should include this object from an archive library.
  2479. template<int size, bool big_endian>
  2480. Archive::Should_include
  2481. Sized_incr_dynobj<size, big_endian>::do_should_include_member(
  2482. Symbol_table*,
  2483. Layout*,
  2484. Read_symbols_data*,
  2485. std::string*)
  2486. {
  2487. gold_unreachable();
  2488. }
  2489. // Iterate over global symbols, calling a visitor class V for each.
  2490. template<int size, bool big_endian>
  2491. void
  2492. Sized_incr_dynobj<size, big_endian>::do_for_all_global_symbols(
  2493. Read_symbols_data*,
  2494. Library_base::Symbol_visitor_base*)
  2495. {
  2496. // This routine is not used for dynamic libraries.
  2497. }
  2498. // Iterate over local symbols, calling a visitor class V for each GOT offset
  2499. // associated with a local symbol.
  2500. template<int size, bool big_endian>
  2501. void
  2502. Sized_incr_dynobj<size, big_endian>::do_for_all_local_got_entries(
  2503. Got_offset_list::Visitor*) const
  2504. {
  2505. }
  2506. // Get the size of a section.
  2507. template<int size, bool big_endian>
  2508. uint64_t
  2509. Sized_incr_dynobj<size, big_endian>::do_section_size(unsigned int)
  2510. {
  2511. gold_unreachable();
  2512. }
  2513. // Get the name of a section.
  2514. template<int size, bool big_endian>
  2515. std::string
  2516. Sized_incr_dynobj<size, big_endian>::do_section_name(unsigned int) const
  2517. {
  2518. gold_unreachable();
  2519. }
  2520. // Return a view of the contents of a section.
  2521. template<int size, bool big_endian>
  2522. const unsigned char*
  2523. Sized_incr_dynobj<size, big_endian>::do_section_contents(
  2524. unsigned int,
  2525. section_size_type*,
  2526. bool)
  2527. {
  2528. gold_unreachable();
  2529. }
  2530. // Return section flags.
  2531. template<int size, bool big_endian>
  2532. uint64_t
  2533. Sized_incr_dynobj<size, big_endian>::do_section_flags(unsigned int)
  2534. {
  2535. gold_unreachable();
  2536. }
  2537. // Return section entsize.
  2538. template<int size, bool big_endian>
  2539. uint64_t
  2540. Sized_incr_dynobj<size, big_endian>::do_section_entsize(unsigned int)
  2541. {
  2542. gold_unreachable();
  2543. }
  2544. // Return section address.
  2545. template<int size, bool big_endian>
  2546. uint64_t
  2547. Sized_incr_dynobj<size, big_endian>::do_section_address(unsigned int)
  2548. {
  2549. gold_unreachable();
  2550. }
  2551. // Return section type.
  2552. template<int size, bool big_endian>
  2553. unsigned int
  2554. Sized_incr_dynobj<size, big_endian>::do_section_type(unsigned int)
  2555. {
  2556. gold_unreachable();
  2557. }
  2558. // Return the section link field.
  2559. template<int size, bool big_endian>
  2560. unsigned int
  2561. Sized_incr_dynobj<size, big_endian>::do_section_link(unsigned int)
  2562. {
  2563. gold_unreachable();
  2564. }
  2565. // Return the section link field.
  2566. template<int size, bool big_endian>
  2567. unsigned int
  2568. Sized_incr_dynobj<size, big_endian>::do_section_info(unsigned int)
  2569. {
  2570. gold_unreachable();
  2571. }
  2572. // Return the section alignment.
  2573. template<int size, bool big_endian>
  2574. uint64_t
  2575. Sized_incr_dynobj<size, big_endian>::do_section_addralign(unsigned int)
  2576. {
  2577. gold_unreachable();
  2578. }
  2579. // Return the Xindex structure to use.
  2580. template<int size, bool big_endian>
  2581. Xindex*
  2582. Sized_incr_dynobj<size, big_endian>::do_initialize_xindex()
  2583. {
  2584. gold_unreachable();
  2585. }
  2586. // Get symbol counts.
  2587. template<int size, bool big_endian>
  2588. void
  2589. Sized_incr_dynobj<size, big_endian>::do_get_global_symbol_counts(
  2590. const Symbol_table*,
  2591. size_t* defined,
  2592. size_t* used) const
  2593. {
  2594. *defined = this->defined_count_;
  2595. size_t count = 0;
  2596. for (typename Symbols::const_iterator p = this->symbols_.begin();
  2597. p != this->symbols_.end();
  2598. ++p)
  2599. if (*p != NULL
  2600. && (*p)->source() == Symbol::FROM_OBJECT
  2601. && (*p)->object() == this
  2602. && (*p)->is_defined()
  2603. && (*p)->dynsym_index() != -1U)
  2604. ++count;
  2605. *used = count;
  2606. }
  2607. // Allocate an incremental object of the appropriate size and endianness.
  2608. Object*
  2609. make_sized_incremental_object(
  2610. Incremental_binary* ibase,
  2611. unsigned int input_file_index,
  2612. Incremental_input_type input_type,
  2613. const Incremental_binary::Input_reader* input_reader)
  2614. {
  2615. Object* obj = NULL;
  2616. std::string name(input_reader->filename());
  2617. switch (parameters->size_and_endianness())
  2618. {
  2619. #ifdef HAVE_TARGET_32_LITTLE
  2620. case Parameters::TARGET_32_LITTLE:
  2621. {
  2622. Sized_incremental_binary<32, false>* sized_ibase =
  2623. static_cast<Sized_incremental_binary<32, false>*>(ibase);
  2624. if (input_type == INCREMENTAL_INPUT_SHARED_LIBRARY)
  2625. obj = new Sized_incr_dynobj<32, false>(name, sized_ibase,
  2626. input_file_index);
  2627. else
  2628. obj = new Sized_relobj_incr<32, false>(name, sized_ibase,
  2629. input_file_index);
  2630. }
  2631. break;
  2632. #endif
  2633. #ifdef HAVE_TARGET_32_BIG
  2634. case Parameters::TARGET_32_BIG:
  2635. {
  2636. Sized_incremental_binary<32, true>* sized_ibase =
  2637. static_cast<Sized_incremental_binary<32, true>*>(ibase);
  2638. if (input_type == INCREMENTAL_INPUT_SHARED_LIBRARY)
  2639. obj = new Sized_incr_dynobj<32, true>(name, sized_ibase,
  2640. input_file_index);
  2641. else
  2642. obj = new Sized_relobj_incr<32, true>(name, sized_ibase,
  2643. input_file_index);
  2644. }
  2645. break;
  2646. #endif
  2647. #ifdef HAVE_TARGET_64_LITTLE
  2648. case Parameters::TARGET_64_LITTLE:
  2649. {
  2650. Sized_incremental_binary<64, false>* sized_ibase =
  2651. static_cast<Sized_incremental_binary<64, false>*>(ibase);
  2652. if (input_type == INCREMENTAL_INPUT_SHARED_LIBRARY)
  2653. obj = new Sized_incr_dynobj<64, false>(name, sized_ibase,
  2654. input_file_index);
  2655. else
  2656. obj = new Sized_relobj_incr<64, false>(name, sized_ibase,
  2657. input_file_index);
  2658. }
  2659. break;
  2660. #endif
  2661. #ifdef HAVE_TARGET_64_BIG
  2662. case Parameters::TARGET_64_BIG:
  2663. {
  2664. Sized_incremental_binary<64, true>* sized_ibase =
  2665. static_cast<Sized_incremental_binary<64, true>*>(ibase);
  2666. if (input_type == INCREMENTAL_INPUT_SHARED_LIBRARY)
  2667. obj = new Sized_incr_dynobj<64, true>(name, sized_ibase,
  2668. input_file_index);
  2669. else
  2670. obj = new Sized_relobj_incr<64, true>(name, sized_ibase,
  2671. input_file_index);
  2672. }
  2673. break;
  2674. #endif
  2675. default:
  2676. gold_unreachable();
  2677. }
  2678. gold_assert(obj != NULL);
  2679. return obj;
  2680. }
  2681. // Copy the unused symbols from the incremental input info.
  2682. // We need to do this because we may be overwriting the incremental
  2683. // input info in the base file before we write the new incremental
  2684. // info.
  2685. void
  2686. Incremental_library::copy_unused_symbols()
  2687. {
  2688. unsigned int symcount = this->input_reader_->get_unused_symbol_count();
  2689. this->unused_symbols_.reserve(symcount);
  2690. for (unsigned int i = 0; i < symcount; ++i)
  2691. {
  2692. std::string name(this->input_reader_->get_unused_symbol(i));
  2693. this->unused_symbols_.push_back(name);
  2694. }
  2695. }
  2696. // Iterator for unused global symbols in the library.
  2697. void
  2698. Incremental_library::do_for_all_unused_symbols(Symbol_visitor_base* v) const
  2699. {
  2700. for (Symbol_list::const_iterator p = this->unused_symbols_.begin();
  2701. p != this->unused_symbols_.end();
  2702. ++p)
  2703. v->visit(p->c_str());
  2704. }
  2705. // Instantiate the templates we need.
  2706. #ifdef HAVE_TARGET_32_LITTLE
  2707. template
  2708. class Sized_incremental_binary<32, false>;
  2709. template
  2710. class Sized_relobj_incr<32, false>;
  2711. template
  2712. class Sized_incr_dynobj<32, false>;
  2713. #endif
  2714. #ifdef HAVE_TARGET_32_BIG
  2715. template
  2716. class Sized_incremental_binary<32, true>;
  2717. template
  2718. class Sized_relobj_incr<32, true>;
  2719. template
  2720. class Sized_incr_dynobj<32, true>;
  2721. #endif
  2722. #ifdef HAVE_TARGET_64_LITTLE
  2723. template
  2724. class Sized_incremental_binary<64, false>;
  2725. template
  2726. class Sized_relobj_incr<64, false>;
  2727. template
  2728. class Sized_incr_dynobj<64, false>;
  2729. #endif
  2730. #ifdef HAVE_TARGET_64_BIG
  2731. template
  2732. class Sized_incremental_binary<64, true>;
  2733. template
  2734. class Sized_relobj_incr<64, true>;
  2735. template
  2736. class Sized_incr_dynobj<64, true>;
  2737. #endif
  2738. } // End namespace gold.