archive.cc 38 KB

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