archive.cc 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347
  1. // archive.cc -- archive support for gold
  2. // Copyright (C) 2006-2015 Free Software Foundation, Inc.
  3. // Written by Ian Lance Taylor <iant@google.com>.
  4. // This file is part of gold.
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation; either version 3 of the License, or
  8. // (at your option) any later version.
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. // MA 02110-1301, USA.
  17. #include "gold.h"
  18. #include <cerrno>
  19. #include <cstring>
  20. #include <climits>
  21. #include <vector>
  22. #include "libiberty.h"
  23. #include "filenames.h"
  24. #include "elfcpp.h"
  25. #include "options.h"
  26. #include "mapfile.h"
  27. #include "fileread.h"
  28. #include "readsyms.h"
  29. #include "symtab.h"
  30. #include "object.h"
  31. #include "layout.h"
  32. #include "archive.h"
  33. #include "plugin.h"
  34. #include "incremental.h"
  35. namespace gold
  36. {
  37. // Library_base methods.
  38. // Determine whether a definition of SYM_NAME should cause an archive
  39. // library member to be included in the link. Returns SHOULD_INCLUDE_YES
  40. // if the symbol is referenced but not defined, SHOULD_INCLUDE_NO if the
  41. // symbol is already defined, and SHOULD_INCLUDE_UNKNOWN if the symbol is
  42. // neither referenced nor defined.
  43. Library_base::Should_include
  44. Library_base::should_include_member(Symbol_table* symtab, Layout* layout,
  45. const char* sym_name, Symbol** symp,
  46. std::string* why, char** tmpbufp,
  47. size_t* tmpbuflen)
  48. {
  49. // In an object file, and therefore in an archive map, an
  50. // '@' in the name separates the symbol name from the
  51. // version name. If there are two '@' characters, this is
  52. // the default version.
  53. char* tmpbuf = *tmpbufp;
  54. const char* ver = strchr(sym_name, '@');
  55. bool def = false;
  56. if (ver != NULL)
  57. {
  58. size_t symlen = ver - sym_name;
  59. if (symlen + 1 > *tmpbuflen)
  60. {
  61. tmpbuf = static_cast<char*>(xrealloc(tmpbuf, symlen + 1));
  62. *tmpbufp = tmpbuf;
  63. *tmpbuflen = symlen + 1;
  64. }
  65. memcpy(tmpbuf, sym_name, symlen);
  66. tmpbuf[symlen] = '\0';
  67. sym_name = tmpbuf;
  68. ++ver;
  69. if (*ver == '@')
  70. {
  71. ++ver;
  72. def = true;
  73. }
  74. }
  75. Symbol* sym = symtab->lookup(sym_name, ver);
  76. if (def
  77. && ver != NULL
  78. && (sym == NULL
  79. || !sym->is_undefined()
  80. || sym->binding() == elfcpp::STB_WEAK))
  81. sym = symtab->lookup(sym_name, NULL);
  82. *symp = sym;
  83. if (sym != NULL)
  84. {
  85. if (!sym->is_undefined())
  86. return Library_base::SHOULD_INCLUDE_NO;
  87. // PR 12001: Do not include an archive when the undefined
  88. // symbol has actually been defined on the command line.
  89. if (layout->script_options()->is_pending_assignment(sym_name))
  90. return Library_base::SHOULD_INCLUDE_NO;
  91. // If the symbol is weak undefined, we still need to check
  92. // for other reasons (like a -u option).
  93. if (sym->binding() != elfcpp::STB_WEAK)
  94. return Library_base::SHOULD_INCLUDE_YES;
  95. }
  96. // Check whether the symbol was named in a -u option.
  97. if (parameters->options().is_undefined(sym_name))
  98. {
  99. *why = "-u ";
  100. *why += sym_name;
  101. return Library_base::SHOULD_INCLUDE_YES;
  102. }
  103. if (parameters->options().is_export_dynamic_symbol(sym_name))
  104. {
  105. *why = "--export-dynamic-symbol ";
  106. *why += sym_name;
  107. return Library_base::SHOULD_INCLUDE_YES;
  108. }
  109. if (layout->script_options()->is_referenced(sym_name))
  110. {
  111. size_t alc = 100 + strlen(sym_name);
  112. char* buf = new char[alc];
  113. snprintf(buf, alc, _("script or expression reference to %s"),
  114. sym_name);
  115. *why = buf;
  116. delete[] buf;
  117. return Library_base::SHOULD_INCLUDE_YES;
  118. }
  119. if (!parameters->options().relocatable())
  120. {
  121. const char* entry_sym = parameters->entry();
  122. if (entry_sym != NULL && strcmp(sym_name, entry_sym) == 0)
  123. {
  124. *why = "entry symbol ";
  125. *why += sym_name;
  126. return Library_base::SHOULD_INCLUDE_YES;
  127. }
  128. }
  129. return Library_base::SHOULD_INCLUDE_UNKNOWN;
  130. }
  131. // The header of an entry in the archive. This is all readable text,
  132. // padded with spaces where necessary. If the contents of an archive
  133. // are all text file, the entire archive is readable.
  134. struct Archive::Archive_header
  135. {
  136. // The entry name.
  137. char ar_name[16];
  138. // The file modification time.
  139. char ar_date[12];
  140. // The user's UID in decimal.
  141. char ar_uid[6];
  142. // The user's GID in decimal.
  143. char ar_gid[6];
  144. // The file mode in octal.
  145. char ar_mode[8];
  146. // The file size in decimal.
  147. char ar_size[10];
  148. // The final magic code.
  149. char ar_fmag[2];
  150. };
  151. // Class Archive static variables.
  152. unsigned int Archive::total_archives;
  153. unsigned int Archive::total_members;
  154. unsigned int Archive::total_members_loaded;
  155. // Archive methods.
  156. const char Archive::armag[sarmag] =
  157. {
  158. '!', '<', 'a', 'r', 'c', 'h', '>', '\n'
  159. };
  160. const char Archive::armagt[sarmag] =
  161. {
  162. '!', '<', 't', 'h', 'i', 'n', '>', '\n'
  163. };
  164. const char Archive::arfmag[2] = { '`', '\n' };
  165. Archive::Archive(const std::string& name, Input_file* input_file,
  166. bool is_thin_archive, Dirsearch* dirpath, Task* task)
  167. : Library_base(task), name_(name), input_file_(input_file), armap_(),
  168. armap_names_(), extended_names_(), armap_checked_(), seen_offsets_(),
  169. members_(), is_thin_archive_(is_thin_archive), included_member_(false),
  170. nested_archives_(), dirpath_(dirpath), num_members_(0),
  171. included_all_members_(false)
  172. {
  173. this->no_export_ =
  174. parameters->options().check_excluded_libs(input_file->found_name());
  175. }
  176. // Set up the archive: read the symbol map and the extended name
  177. // table.
  178. void
  179. Archive::setup()
  180. {
  181. // We need to ignore empty archives.
  182. if (this->input_file_->file().filesize() == sarmag)
  183. return;
  184. // The first member of the archive should be the symbol table.
  185. std::string armap_name;
  186. off_t header_size = this->read_header(sarmag, false, &armap_name, NULL);
  187. if (header_size == -1)
  188. return;
  189. section_size_type armap_size = convert_to_section_size_type(header_size);
  190. off_t off = sarmag;
  191. if (armap_name.empty())
  192. {
  193. this->read_armap(sarmag + sizeof(Archive_header), armap_size);
  194. off = sarmag + sizeof(Archive_header) + armap_size;
  195. }
  196. else if (!this->input_file_->options().whole_archive())
  197. gold_error(_("%s: no archive symbol table (run ranlib)"),
  198. this->name().c_str());
  199. // See if there is an extended name table. We cache these views
  200. // because it is likely that we will want to read the following
  201. // header in the add_symbols routine.
  202. if ((off & 1) != 0)
  203. ++off;
  204. std::string xname;
  205. header_size = this->read_header(off, true, &xname, NULL);
  206. if (header_size == -1)
  207. return;
  208. section_size_type extended_size = convert_to_section_size_type(header_size);
  209. if (xname == "/")
  210. {
  211. const unsigned char* p = this->get_view(off + sizeof(Archive_header),
  212. extended_size, false, true);
  213. const char* px = reinterpret_cast<const char*>(p);
  214. this->extended_names_.assign(px, extended_size);
  215. }
  216. bool preread_syms = (parameters->options().threads()
  217. && parameters->options().preread_archive_symbols());
  218. #ifndef ENABLE_THREADS
  219. preread_syms = false;
  220. #else
  221. if (parameters->options().has_plugins())
  222. preread_syms = false;
  223. #endif
  224. if (preread_syms)
  225. this->read_all_symbols();
  226. }
  227. // Unlock any nested archives.
  228. void
  229. Archive::unlock_nested_archives()
  230. {
  231. for (Nested_archive_table::iterator p = this->nested_archives_.begin();
  232. p != this->nested_archives_.end();
  233. ++p)
  234. {
  235. p->second->unlock(this->task_);
  236. }
  237. }
  238. // Read the archive symbol map.
  239. void
  240. Archive::read_armap(off_t start, section_size_type size)
  241. {
  242. // To count the total number of archive members, we'll just count
  243. // the number of times the file offset changes. Since most archives
  244. // group the symbols in the armap by object, this ought to give us
  245. // an accurate count.
  246. off_t last_seen_offset = -1;
  247. // Read in the entire armap.
  248. const unsigned char* p = this->get_view(start, size, true, false);
  249. // Numbers in the armap are always big-endian.
  250. const elfcpp::Elf_Word* pword = reinterpret_cast<const elfcpp::Elf_Word*>(p);
  251. unsigned int nsyms = elfcpp::Swap<32, true>::readval(pword);
  252. ++pword;
  253. // Note that the addition is in units of sizeof(elfcpp::Elf_Word).
  254. const char* pnames = reinterpret_cast<const char*>(pword + nsyms);
  255. section_size_type names_size =
  256. reinterpret_cast<const char*>(p) + size - pnames;
  257. this->armap_names_.assign(pnames, names_size);
  258. this->armap_.resize(nsyms);
  259. section_offset_type name_offset = 0;
  260. for (unsigned int i = 0; i < nsyms; ++i)
  261. {
  262. this->armap_[i].name_offset = name_offset;
  263. this->armap_[i].file_offset = elfcpp::Swap<32, true>::readval(pword);
  264. name_offset += strlen(pnames + name_offset) + 1;
  265. ++pword;
  266. if (this->armap_[i].file_offset != last_seen_offset)
  267. {
  268. last_seen_offset = this->armap_[i].file_offset;
  269. ++this->num_members_;
  270. }
  271. }
  272. if (static_cast<section_size_type>(name_offset) > names_size)
  273. gold_error(_("%s: bad archive symbol table names"),
  274. this->name().c_str());
  275. // This array keeps track of which symbols are for archive elements
  276. // which we have already included in the link.
  277. this->armap_checked_.resize(nsyms);
  278. }
  279. // Read the header of an archive member at OFF. Fail if something
  280. // goes wrong. Return the size of the member. Set *PNAME to the name
  281. // of the member.
  282. off_t
  283. Archive::read_header(off_t off, bool cache, std::string* pname,
  284. off_t* nested_off)
  285. {
  286. const unsigned char* p = this->get_view(off, sizeof(Archive_header), true,
  287. cache);
  288. const Archive_header* hdr = reinterpret_cast<const Archive_header*>(p);
  289. return this->interpret_header(hdr, off, pname, nested_off);
  290. }
  291. // Interpret the header of HDR, the header of the archive member at
  292. // file offset OFF. Return the size of the member, or -1 if something
  293. // has gone wrong. Set *PNAME to the name of the member.
  294. off_t
  295. Archive::interpret_header(const Archive_header* hdr, off_t off,
  296. std::string* pname, off_t* nested_off) const
  297. {
  298. if (memcmp(hdr->ar_fmag, arfmag, sizeof arfmag) != 0)
  299. {
  300. gold_error(_("%s: malformed archive header at %zu"),
  301. this->name().c_str(), static_cast<size_t>(off));
  302. return -1;
  303. }
  304. const int size_string_size = sizeof hdr->ar_size;
  305. char size_string[size_string_size + 1];
  306. memcpy(size_string, hdr->ar_size, size_string_size);
  307. char* ps = size_string + size_string_size;
  308. while (ps[-1] == ' ')
  309. --ps;
  310. *ps = '\0';
  311. errno = 0;
  312. char* end;
  313. off_t member_size = strtol(size_string, &end, 10);
  314. if (*end != '\0'
  315. || member_size < 0
  316. || (member_size == LONG_MAX && errno == ERANGE))
  317. {
  318. gold_error(_("%s: malformed archive header size at %zu"),
  319. this->name().c_str(), static_cast<size_t>(off));
  320. return -1;
  321. }
  322. if (hdr->ar_name[0] != '/')
  323. {
  324. const char* name_end = strchr(hdr->ar_name, '/');
  325. if (name_end == NULL
  326. || name_end - hdr->ar_name >= static_cast<int>(sizeof hdr->ar_name))
  327. {
  328. gold_error(_("%s: malformed archive header name at %zu"),
  329. this->name().c_str(), static_cast<size_t>(off));
  330. return -1;
  331. }
  332. pname->assign(hdr->ar_name, name_end - hdr->ar_name);
  333. if (nested_off != NULL)
  334. *nested_off = 0;
  335. }
  336. else if (hdr->ar_name[1] == ' ')
  337. {
  338. // This is the symbol table.
  339. if (!pname->empty())
  340. pname->clear();
  341. }
  342. else if (hdr->ar_name[1] == '/')
  343. {
  344. // This is the extended name table.
  345. pname->assign(1, '/');
  346. }
  347. else
  348. {
  349. errno = 0;
  350. long x = strtol(hdr->ar_name + 1, &end, 10);
  351. long y = 0;
  352. if (*end == ':')
  353. y = strtol(end + 1, &end, 10);
  354. if (*end != ' '
  355. || x < 0
  356. || (x == LONG_MAX && errno == ERANGE)
  357. || static_cast<size_t>(x) >= this->extended_names_.size())
  358. {
  359. gold_error(_("%s: bad extended name index at %zu"),
  360. this->name().c_str(), static_cast<size_t>(off));
  361. return -1;
  362. }
  363. const char* name = this->extended_names_.data() + x;
  364. const char* name_end = strchr(name, '\n');
  365. if (static_cast<size_t>(name_end - name) > this->extended_names_.size()
  366. || name_end[-1] != '/')
  367. {
  368. gold_error(_("%s: bad extended name entry at header %zu"),
  369. this->name().c_str(), static_cast<size_t>(off));
  370. return -1;
  371. }
  372. pname->assign(name, name_end - 1 - name);
  373. if (nested_off != NULL)
  374. *nested_off = y;
  375. }
  376. return member_size;
  377. }
  378. // An archive member iterator.
  379. class Archive::const_iterator
  380. {
  381. public:
  382. // The header of an archive member. This is what this iterator
  383. // points to.
  384. struct Header
  385. {
  386. // The name of the member.
  387. std::string name;
  388. // The file offset of the member.
  389. off_t off;
  390. // The file offset of a nested archive member.
  391. off_t nested_off;
  392. // The size of the member.
  393. off_t size;
  394. };
  395. const_iterator(Archive* archive, off_t off)
  396. : archive_(archive), off_(off)
  397. { this->read_next_header(); }
  398. const Header&
  399. operator*() const
  400. { return this->header_; }
  401. const Header*
  402. operator->() const
  403. { return &this->header_; }
  404. const_iterator&
  405. operator++()
  406. {
  407. if (this->off_ == this->archive_->file().filesize())
  408. return *this;
  409. this->off_ += sizeof(Archive_header);
  410. if (!this->archive_->is_thin_archive())
  411. this->off_ += this->header_.size;
  412. if ((this->off_ & 1) != 0)
  413. ++this->off_;
  414. this->read_next_header();
  415. return *this;
  416. }
  417. const_iterator
  418. operator++(int)
  419. {
  420. const_iterator ret = *this;
  421. ++*this;
  422. return ret;
  423. }
  424. bool
  425. operator==(const const_iterator p) const
  426. { return this->off_ == p->off; }
  427. bool
  428. operator!=(const const_iterator p) const
  429. { return this->off_ != p->off; }
  430. private:
  431. void
  432. read_next_header();
  433. // The underlying archive.
  434. Archive* archive_;
  435. // The current offset in the file.
  436. off_t off_;
  437. // The current archive header.
  438. Header header_;
  439. };
  440. // Read the next archive header.
  441. void
  442. Archive::const_iterator::read_next_header()
  443. {
  444. off_t filesize = this->archive_->file().filesize();
  445. while (true)
  446. {
  447. if (filesize - this->off_ < static_cast<off_t>(sizeof(Archive_header)))
  448. {
  449. if (filesize != this->off_)
  450. {
  451. gold_error(_("%s: short archive header at %zu"),
  452. this->archive_->filename().c_str(),
  453. static_cast<size_t>(this->off_));
  454. this->off_ = filesize;
  455. }
  456. this->header_.off = filesize;
  457. return;
  458. }
  459. unsigned char buf[sizeof(Archive_header)];
  460. this->archive_->file().read(this->off_, sizeof(Archive_header), buf);
  461. const Archive_header* hdr = reinterpret_cast<const Archive_header*>(buf);
  462. off_t size = this->archive_->interpret_header(hdr, this->off_,
  463. &this->header_.name,
  464. &this->header_.nested_off);
  465. if (size == -1)
  466. {
  467. this->header_.off = filesize;
  468. return;
  469. }
  470. this->header_.size = size;
  471. this->header_.off = this->off_;
  472. // Skip special members.
  473. if (!this->header_.name.empty() && this->header_.name != "/")
  474. return;
  475. this->off_ += sizeof(Archive_header) + this->header_.size;
  476. if ((this->off_ & 1) != 0)
  477. ++this->off_;
  478. }
  479. }
  480. // Initial iterator.
  481. Archive::const_iterator
  482. Archive::begin()
  483. {
  484. return Archive::const_iterator(this, sarmag);
  485. }
  486. // Final iterator.
  487. Archive::const_iterator
  488. Archive::end()
  489. {
  490. return Archive::const_iterator(this, this->input_file_->file().filesize());
  491. }
  492. // Get the file and offset for an archive member, which may be an
  493. // external member of a thin archive. Set *INPUT_FILE to the
  494. // file containing the actual member, *MEMOFF to the offset
  495. // within that file (0 if not a nested archive), and *MEMBER_NAME
  496. // to the name of the archive member. Return TRUE on success.
  497. bool
  498. Archive::get_file_and_offset(off_t off, Input_file** input_file, off_t* memoff,
  499. off_t* memsize, std::string* member_name)
  500. {
  501. off_t nested_off;
  502. *memsize = this->read_header(off, false, member_name, &nested_off);
  503. if (*memsize == -1)
  504. return false;
  505. *input_file = this->input_file_;
  506. *memoff = off + static_cast<off_t>(sizeof(Archive_header));
  507. if (!this->is_thin_archive_)
  508. return true;
  509. // Adjust a relative pathname so that it is relative
  510. // to the directory containing the archive.
  511. if (!IS_ABSOLUTE_PATH(member_name->c_str()))
  512. {
  513. const char* arch_path = this->filename().c_str();
  514. const char* basename = lbasename(arch_path);
  515. if (basename > arch_path)
  516. member_name->replace(0, 0,
  517. this->filename().substr(0, basename - arch_path));
  518. }
  519. if (nested_off > 0)
  520. {
  521. // This is a member of a nested archive. Open the containing
  522. // archive if we don't already have it open, then do a recursive
  523. // call to include the member from that archive.
  524. Archive* arch;
  525. Nested_archive_table::const_iterator p =
  526. this->nested_archives_.find(*member_name);
  527. if (p != this->nested_archives_.end())
  528. arch = p->second;
  529. else
  530. {
  531. Input_file_argument* input_file_arg =
  532. new Input_file_argument(member_name->c_str(),
  533. Input_file_argument::INPUT_FILE_TYPE_FILE,
  534. "", false, parameters->options());
  535. *input_file = new Input_file(input_file_arg);
  536. int dummy = 0;
  537. if (!(*input_file)->open(*this->dirpath_, this->task_, &dummy))
  538. return false;
  539. arch = new Archive(*member_name, *input_file, false, this->dirpath_,
  540. this->task_);
  541. arch->setup();
  542. std::pair<Nested_archive_table::iterator, bool> ins =
  543. this->nested_archives_.insert(std::make_pair(*member_name, arch));
  544. gold_assert(ins.second);
  545. }
  546. return arch->get_file_and_offset(nested_off, input_file, memoff,
  547. memsize, member_name);
  548. }
  549. // This is an external member of a thin archive. Open the
  550. // file as a regular relocatable object file.
  551. Input_file_argument* input_file_arg =
  552. new Input_file_argument(member_name->c_str(),
  553. Input_file_argument::INPUT_FILE_TYPE_FILE,
  554. "", false, this->input_file_->options());
  555. *input_file = new Input_file(input_file_arg);
  556. int dummy = 0;
  557. if (!(*input_file)->open(*this->dirpath_, this->task_, &dummy))
  558. return false;
  559. *memoff = 0;
  560. *memsize = (*input_file)->file().filesize();
  561. return true;
  562. }
  563. // Return an ELF object for the member at offset OFF. If
  564. // PUNCONFIGURED is not NULL, then if the ELF object has an
  565. // unsupported target type, set *PUNCONFIGURED to true and return
  566. // NULL.
  567. Object*
  568. Archive::get_elf_object_for_member(off_t off, bool* punconfigured)
  569. {
  570. if (punconfigured != NULL)
  571. *punconfigured = false;
  572. Input_file* input_file;
  573. off_t memoff;
  574. off_t memsize;
  575. std::string member_name;
  576. if (!this->get_file_and_offset(off, &input_file, &memoff, &memsize,
  577. &member_name))
  578. return NULL;
  579. const unsigned char* ehdr;
  580. int read_size;
  581. Object *obj = NULL;
  582. bool is_elf_obj = false;
  583. if (is_elf_object(input_file, memoff, &ehdr, &read_size))
  584. {
  585. obj = make_elf_object((std::string(this->input_file_->filename())
  586. + "(" + member_name + ")"),
  587. input_file, memoff, ehdr, read_size,
  588. punconfigured);
  589. is_elf_obj = true;
  590. }
  591. if (parameters->options().has_plugins())
  592. {
  593. Object* plugin_obj
  594. = parameters->options().plugins()->claim_file(input_file,
  595. memoff,
  596. memsize,
  597. obj);
  598. if (plugin_obj != NULL)
  599. {
  600. // The input file was claimed by a plugin, and its symbols
  601. // have been provided by the plugin.
  602. // Delete its elf object.
  603. if (obj != NULL)
  604. delete obj;
  605. return plugin_obj;
  606. }
  607. }
  608. if (!is_elf_obj)
  609. {
  610. gold_error(_("%s: member at %zu is not an ELF object"),
  611. this->name().c_str(), static_cast<size_t>(off));
  612. return NULL;
  613. }
  614. if (obj == NULL)
  615. return NULL;
  616. obj->set_no_export(this->no_export());
  617. return obj;
  618. }
  619. // Read the symbols from all the archive members in the link.
  620. void
  621. Archive::read_all_symbols()
  622. {
  623. for (Archive::const_iterator p = this->begin();
  624. p != this->end();
  625. ++p)
  626. this->read_symbols(p->off);
  627. }
  628. // Read the symbols from an archive member in the link. OFF is the file
  629. // offset of the member header.
  630. void
  631. Archive::read_symbols(off_t off)
  632. {
  633. Object* obj = this->get_elf_object_for_member(off, NULL);
  634. if (obj == NULL)
  635. return;
  636. Read_symbols_data* sd = new Read_symbols_data;
  637. obj->read_symbols(sd);
  638. Archive_member member(obj, sd);
  639. this->members_[off] = member;
  640. }
  641. // Select members from the archive and add them to the link. We walk
  642. // through the elements in the archive map, and look each one up in
  643. // the symbol table. If it exists as a strong undefined symbol, we
  644. // pull in the corresponding element. We have to do this in a loop,
  645. // since pulling in one element may create new undefined symbols which
  646. // may be satisfied by other objects in the archive. Return true in
  647. // the normal case, false if the first member we tried to add from
  648. // this archive had an incompatible target.
  649. bool
  650. Archive::add_symbols(Symbol_table* symtab, Layout* layout,
  651. Input_objects* input_objects, Mapfile* mapfile)
  652. {
  653. ++Archive::total_archives;
  654. if (this->input_file_->options().whole_archive())
  655. return this->include_all_members(symtab, layout, input_objects,
  656. mapfile);
  657. Archive::total_members += this->num_members_;
  658. input_objects->archive_start(this);
  659. const size_t armap_size = this->armap_.size();
  660. // This is a quick optimization, since we usually see many symbols
  661. // in a row with the same offset. last_seen_offset holds the last
  662. // offset we saw that was present in the seen_offsets_ set.
  663. off_t last_seen_offset = -1;
  664. // Track which symbols in the symbol table we've already found to be
  665. // defined.
  666. char* tmpbuf = NULL;
  667. size_t tmpbuflen = 0;
  668. bool added_new_object;
  669. do
  670. {
  671. added_new_object = false;
  672. for (size_t i = 0; i < armap_size; ++i)
  673. {
  674. if (this->armap_checked_[i])
  675. continue;
  676. if (this->armap_[i].file_offset == last_seen_offset)
  677. {
  678. this->armap_checked_[i] = true;
  679. continue;
  680. }
  681. if (this->seen_offsets_.find(this->armap_[i].file_offset)
  682. != this->seen_offsets_.end())
  683. {
  684. this->armap_checked_[i] = true;
  685. last_seen_offset = this->armap_[i].file_offset;
  686. continue;
  687. }
  688. const char* sym_name = (this->armap_names_.data()
  689. + this->armap_[i].name_offset);
  690. Symbol* sym;
  691. std::string why;
  692. Archive::Should_include t =
  693. Archive::should_include_member(symtab, layout, sym_name, &sym,
  694. &why, &tmpbuf, &tmpbuflen);
  695. if (t == Archive::SHOULD_INCLUDE_NO
  696. || t == Archive::SHOULD_INCLUDE_YES)
  697. this->armap_checked_[i] = true;
  698. if (t != Archive::SHOULD_INCLUDE_YES)
  699. continue;
  700. // We want to include this object in the link.
  701. last_seen_offset = this->armap_[i].file_offset;
  702. this->seen_offsets_.insert(last_seen_offset);
  703. if (!this->include_member(symtab, layout, input_objects,
  704. last_seen_offset, mapfile, sym,
  705. why.c_str()))
  706. {
  707. if (tmpbuf != NULL)
  708. free(tmpbuf);
  709. return false;
  710. }
  711. added_new_object = true;
  712. }
  713. }
  714. while (added_new_object);
  715. if (tmpbuf != NULL)
  716. free(tmpbuf);
  717. input_objects->archive_stop(this);
  718. return true;
  719. }
  720. // Return whether the archive includes a member which defines the
  721. // symbol SYM.
  722. bool
  723. Archive::defines_symbol(Symbol* sym) const
  724. {
  725. const char* symname = sym->name();
  726. size_t symname_len = strlen(symname);
  727. size_t armap_size = this->armap_.size();
  728. for (size_t i = 0; i < armap_size; ++i)
  729. {
  730. if (this->armap_checked_[i])
  731. continue;
  732. const char* archive_symname = (this->armap_names_.data()
  733. + this->armap_[i].name_offset);
  734. if (strncmp(archive_symname, symname, symname_len) != 0)
  735. continue;
  736. char c = archive_symname[symname_len];
  737. if (c == '\0' && sym->version() == NULL)
  738. return true;
  739. if (c == '@')
  740. {
  741. const char* ver = archive_symname + symname_len + 1;
  742. if (*ver == '@')
  743. {
  744. if (sym->version() == NULL)
  745. return true;
  746. ++ver;
  747. }
  748. if (sym->version() != NULL && strcmp(sym->version(), ver) == 0)
  749. return true;
  750. }
  751. }
  752. return false;
  753. }
  754. // Include all the archive members in the link. This is for --whole-archive.
  755. bool
  756. Archive::include_all_members(Symbol_table* symtab, Layout* layout,
  757. Input_objects* input_objects, Mapfile* mapfile)
  758. {
  759. // Don't include the same archive twice. This can happen if
  760. // --whole-archive is nested inside --start-group (PR gold/12163).
  761. if (this->included_all_members_)
  762. return true;
  763. this->included_all_members_ = true;
  764. input_objects->archive_start(this);
  765. if (this->members_.size() > 0)
  766. {
  767. std::map<off_t, Archive_member>::const_iterator p;
  768. for (p = this->members_.begin();
  769. p != this->members_.end();
  770. ++p)
  771. {
  772. if (!this->include_member(symtab, layout, input_objects, p->first,
  773. mapfile, NULL, "--whole-archive"))
  774. return false;
  775. ++Archive::total_members;
  776. }
  777. }
  778. else
  779. {
  780. for (Archive::const_iterator p = this->begin();
  781. p != this->end();
  782. ++p)
  783. {
  784. if (!this->include_member(symtab, layout, input_objects, p->off,
  785. mapfile, NULL, "--whole-archive"))
  786. return false;
  787. ++Archive::total_members;
  788. }
  789. }
  790. input_objects->archive_stop(this);
  791. return true;
  792. }
  793. // Return the number of members in the archive. This is only used for
  794. // reports.
  795. size_t
  796. Archive::count_members()
  797. {
  798. size_t ret = 0;
  799. for (Archive::const_iterator p = this->begin();
  800. p != this->end();
  801. ++p)
  802. ++ret;
  803. return ret;
  804. }
  805. // RAII class to ensure we unlock the object if it's a member of a
  806. // thin archive. We can't use Task_lock_obj in Archive::include_member
  807. // because the object file is already locked when it's opened by
  808. // get_elf_object_for_member.
  809. class Thin_archive_object_unlocker
  810. {
  811. public:
  812. Thin_archive_object_unlocker(const Task *task, Object* obj)
  813. : task_(task), obj_(obj)
  814. { }
  815. ~Thin_archive_object_unlocker()
  816. {
  817. if (this->obj_->offset() == 0)
  818. this->obj_->unlock(this->task_);
  819. }
  820. private:
  821. Thin_archive_object_unlocker(const Thin_archive_object_unlocker&);
  822. Thin_archive_object_unlocker& operator=(const Thin_archive_object_unlocker&);
  823. const Task* task_;
  824. Object* obj_;
  825. };
  826. // Include an archive member in the link. OFF is the file offset of
  827. // the member header. WHY is the reason we are including this member.
  828. // Return true if we added the member or if we had an error, return
  829. // false if this was the first member we tried to add from this
  830. // archive and it had an incompatible format.
  831. bool
  832. Archive::include_member(Symbol_table* symtab, Layout* layout,
  833. Input_objects* input_objects, off_t off,
  834. Mapfile* mapfile, Symbol* sym, const char* why)
  835. {
  836. ++Archive::total_members_loaded;
  837. std::map<off_t, Archive_member>::const_iterator p = this->members_.find(off);
  838. if (p != this->members_.end())
  839. {
  840. Object* obj = p->second.obj_;
  841. Read_symbols_data* sd = p->second.sd_;
  842. if (mapfile != NULL)
  843. mapfile->report_include_archive_member(obj->name(), sym, why);
  844. if (input_objects->add_object(obj))
  845. {
  846. obj->layout(symtab, layout, sd);
  847. obj->add_symbols(symtab, sd, layout);
  848. this->included_member_ = true;
  849. }
  850. delete sd;
  851. return true;
  852. }
  853. // If this is the first object we are including from this archive,
  854. // and we searched for this archive, most likely because it was
  855. // found via a -l option, then if the target is incompatible we want
  856. // to move on to the next archive found in the search path.
  857. bool unconfigured = false;
  858. bool* punconfigured = NULL;
  859. if (!this->included_member_ && this->searched_for())
  860. punconfigured = &unconfigured;
  861. Object* obj = this->get_elf_object_for_member(off, punconfigured);
  862. if (obj == NULL)
  863. {
  864. // Return false to search for another archive, true if we found
  865. // an error.
  866. return unconfigured ? false : true;
  867. }
  868. // If the object is an external member of a thin archive,
  869. // unlock it when we're done here.
  870. Thin_archive_object_unlocker unlocker(this->task_, obj);
  871. if (mapfile != NULL)
  872. mapfile->report_include_archive_member(obj->name(), sym, why);
  873. Pluginobj* pluginobj = obj->pluginobj();
  874. if (pluginobj != NULL)
  875. {
  876. pluginobj->add_symbols(symtab, NULL, layout);
  877. this->included_member_ = true;
  878. return true;
  879. }
  880. if (!input_objects->add_object(obj))
  881. {
  882. delete obj;
  883. return true;
  884. }
  885. if (layout->incremental_inputs() != NULL)
  886. layout->incremental_inputs()->report_object(obj, 0, this, NULL);
  887. {
  888. Read_symbols_data sd;
  889. obj->read_symbols(&sd);
  890. obj->layout(symtab, layout, &sd);
  891. obj->add_symbols(symtab, &sd, layout);
  892. }
  893. this->included_member_ = true;
  894. return true;
  895. }
  896. // Iterate over all unused symbols, and call the visitor class V for each.
  897. void
  898. Archive::do_for_all_unused_symbols(Symbol_visitor_base* v) const
  899. {
  900. for (std::vector<Armap_entry>::const_iterator p = this->armap_.begin();
  901. p != this->armap_.end();
  902. ++p)
  903. {
  904. if (this->seen_offsets_.find(p->file_offset)
  905. == this->seen_offsets_.end())
  906. v->visit(this->armap_names_.data() + p->name_offset);
  907. }
  908. }
  909. // Print statistical information to stderr. This is used for --stats.
  910. void
  911. Archive::print_stats()
  912. {
  913. fprintf(stderr, _("%s: archive libraries: %u\n"),
  914. program_name, Archive::total_archives);
  915. fprintf(stderr, _("%s: total archive members: %u\n"),
  916. program_name, Archive::total_members);
  917. fprintf(stderr, _("%s: loaded archive members: %u\n"),
  918. program_name, Archive::total_members_loaded);
  919. }
  920. // Add_archive_symbols methods.
  921. Add_archive_symbols::~Add_archive_symbols()
  922. {
  923. if (this->this_blocker_ != NULL)
  924. delete this->this_blocker_;
  925. // next_blocker_ is deleted by the task associated with the next
  926. // input file.
  927. }
  928. // Return whether we can add the archive symbols. We are blocked by
  929. // this_blocker_. We block next_blocker_. We also lock the file.
  930. Task_token*
  931. Add_archive_symbols::is_runnable()
  932. {
  933. if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
  934. return this->this_blocker_;
  935. return NULL;
  936. }
  937. void
  938. Add_archive_symbols::locks(Task_locker* tl)
  939. {
  940. tl->add(this, this->next_blocker_);
  941. tl->add(this, this->archive_->token());
  942. }
  943. void
  944. Add_archive_symbols::run(Workqueue* workqueue)
  945. {
  946. // For an incremental link, begin recording layout information.
  947. Incremental_inputs* incremental_inputs = this->layout_->incremental_inputs();
  948. if (incremental_inputs != NULL)
  949. {
  950. unsigned int arg_serial = this->input_argument_->file().arg_serial();
  951. Script_info* script_info = this->input_argument_->script_info();
  952. incremental_inputs->report_archive_begin(this->archive_, arg_serial,
  953. script_info);
  954. }
  955. bool added = this->archive_->add_symbols(this->symtab_, this->layout_,
  956. this->input_objects_,
  957. this->mapfile_);
  958. this->archive_->unlock_nested_archives();
  959. this->archive_->release();
  960. this->archive_->clear_uncached_views();
  961. if (!added)
  962. {
  963. // This archive holds object files which are incompatible with
  964. // our output file.
  965. Read_symbols::incompatible_warning(this->input_argument_,
  966. this->archive_->input_file());
  967. Read_symbols::requeue(workqueue, this->input_objects_, this->symtab_,
  968. this->layout_, this->dirpath_, this->dirindex_,
  969. this->mapfile_, this->input_argument_,
  970. this->input_group_, this->next_blocker_);
  971. delete this->archive_;
  972. return;
  973. }
  974. if (this->input_group_ != NULL)
  975. this->input_group_->add_archive(this->archive_);
  976. else
  977. {
  978. // For an incremental link, finish recording the layout information.
  979. if (incremental_inputs != NULL)
  980. incremental_inputs->report_archive_end(this->archive_);
  981. if (!parameters->options().has_plugins()
  982. || this->archive_->input_file()->options().whole_archive())
  983. {
  984. // We no longer need to know about this archive.
  985. delete this->archive_;
  986. }
  987. else
  988. {
  989. // The plugin interface may want to rescan this archive.
  990. parameters->options().plugins()->save_archive(this->archive_);
  991. }
  992. this->archive_ = NULL;
  993. }
  994. }
  995. // Class Lib_group static variables.
  996. unsigned int Lib_group::total_lib_groups;
  997. unsigned int Lib_group::total_members;
  998. unsigned int Lib_group::total_members_loaded;
  999. Lib_group::Lib_group(const Input_file_lib* lib, Task* task)
  1000. : Library_base(task), members_()
  1001. {
  1002. this->members_.resize(lib->size());
  1003. }
  1004. const std::string&
  1005. Lib_group::do_filename() const
  1006. {
  1007. std::string *filename = new std::string("/group/");
  1008. return *filename;
  1009. }
  1010. // Select members from the lib group and add them to the link. We walk
  1011. // through the members, and check if each one up should be included.
  1012. // If the object says it should be included, we do so. We have to do
  1013. // this in a loop, since including one member may create new undefined
  1014. // symbols which may be satisfied by other members.
  1015. void
  1016. Lib_group::add_symbols(Symbol_table* symtab, Layout* layout,
  1017. Input_objects* input_objects)
  1018. {
  1019. ++Lib_group::total_lib_groups;
  1020. Lib_group::total_members += this->members_.size();
  1021. bool added_new_object;
  1022. do
  1023. {
  1024. added_new_object = false;
  1025. unsigned int i = 0;
  1026. while (i < this->members_.size())
  1027. {
  1028. const Archive_member& member = this->members_[i];
  1029. Object* obj = member.obj_;
  1030. std::string why;
  1031. // Skip files with no symbols. Plugin objects have
  1032. // member.sd_ == NULL.
  1033. if (obj != NULL
  1034. && (member.sd_ == NULL || member.sd_->symbol_names != NULL))
  1035. {
  1036. Archive::Should_include t = obj->should_include_member(symtab,
  1037. layout,
  1038. member.sd_,
  1039. &why);
  1040. if (t != Archive::SHOULD_INCLUDE_YES)
  1041. {
  1042. ++i;
  1043. continue;
  1044. }
  1045. this->include_member(symtab, layout, input_objects, member);
  1046. added_new_object = true;
  1047. }
  1048. else
  1049. {
  1050. if (member.sd_ != NULL)
  1051. {
  1052. // The file must be locked in order to destroy the views
  1053. // associated with it.
  1054. gold_assert(obj != NULL);
  1055. obj->lock(this->task_);
  1056. delete member.sd_;
  1057. obj->unlock(this->task_);
  1058. }
  1059. }
  1060. this->members_[i] = this->members_.back();
  1061. this->members_.pop_back();
  1062. }
  1063. }
  1064. while (added_new_object);
  1065. }
  1066. // Include a lib group member in the link.
  1067. void
  1068. Lib_group::include_member(Symbol_table* symtab, Layout* layout,
  1069. Input_objects* input_objects,
  1070. const Archive_member& member)
  1071. {
  1072. ++Lib_group::total_members_loaded;
  1073. Object* obj = member.obj_;
  1074. gold_assert(obj != NULL);
  1075. Pluginobj* pluginobj = obj->pluginobj();
  1076. if (pluginobj != NULL)
  1077. {
  1078. pluginobj->add_symbols(symtab, NULL, layout);
  1079. return;
  1080. }
  1081. Read_symbols_data* sd = member.sd_;
  1082. gold_assert(sd != NULL);
  1083. obj->lock(this->task_);
  1084. if (input_objects->add_object(obj))
  1085. {
  1086. if (layout->incremental_inputs() != NULL)
  1087. layout->incremental_inputs()->report_object(obj, member.arg_serial_,
  1088. this, NULL);
  1089. obj->layout(symtab, layout, sd);
  1090. obj->add_symbols(symtab, sd, layout);
  1091. }
  1092. delete sd;
  1093. // Unlock the file for the next task.
  1094. obj->unlock(this->task_);
  1095. }
  1096. // Iterate over all unused symbols, and call the visitor class V for each.
  1097. void
  1098. Lib_group::do_for_all_unused_symbols(Symbol_visitor_base* v) const
  1099. {
  1100. // Files are removed from the members list when used, so all the
  1101. // files remaining on the list are unused.
  1102. for (std::vector<Archive_member>::const_iterator p = this->members_.begin();
  1103. p != this->members_.end();
  1104. ++p)
  1105. {
  1106. Object* obj = p->obj_;
  1107. obj->for_all_global_symbols(p->sd_, v);
  1108. }
  1109. }
  1110. // Print statistical information to stderr. This is used for --stats.
  1111. void
  1112. Lib_group::print_stats()
  1113. {
  1114. fprintf(stderr, _("%s: lib groups: %u\n"),
  1115. program_name, Lib_group::total_lib_groups);
  1116. fprintf(stderr, _("%s: total lib groups members: %u\n"),
  1117. program_name, Lib_group::total_members);
  1118. fprintf(stderr, _("%s: loaded lib groups members: %u\n"),
  1119. program_name, Lib_group::total_members_loaded);
  1120. }
  1121. Task_token*
  1122. Add_lib_group_symbols::is_runnable()
  1123. {
  1124. if (this->readsyms_blocker_ != NULL && this->readsyms_blocker_->is_blocked())
  1125. return this->readsyms_blocker_;
  1126. if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
  1127. return this->this_blocker_;
  1128. return NULL;
  1129. }
  1130. void
  1131. Add_lib_group_symbols::locks(Task_locker* tl)
  1132. {
  1133. tl->add(this, this->next_blocker_);
  1134. }
  1135. void
  1136. Add_lib_group_symbols::run(Workqueue*)
  1137. {
  1138. // For an incremental link, begin recording layout information.
  1139. Incremental_inputs* incremental_inputs = this->layout_->incremental_inputs();
  1140. if (incremental_inputs != NULL)
  1141. incremental_inputs->report_archive_begin(this->lib_, 0, NULL);
  1142. this->lib_->add_symbols(this->symtab_, this->layout_, this->input_objects_);
  1143. if (incremental_inputs != NULL)
  1144. incremental_inputs->report_archive_end(this->lib_);
  1145. }
  1146. Add_lib_group_symbols::~Add_lib_group_symbols()
  1147. {
  1148. if (this->this_blocker_ != NULL)
  1149. delete this->this_blocker_;
  1150. // next_blocker_ is deleted by the task associated with the next
  1151. // input file.
  1152. }
  1153. } // End namespace gold.