archive.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. // archive.h -- archive support for gold -*- C++ -*-
  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. #ifndef GOLD_ARCHIVE_H
  18. #define GOLD_ARCHIVE_H
  19. #include <string>
  20. #include <vector>
  21. #include "fileread.h"
  22. #include "workqueue.h"
  23. namespace gold
  24. {
  25. class Task;
  26. class Input_argument;
  27. class Input_file;
  28. class Input_objects;
  29. class Input_group;
  30. class Layout;
  31. class Symbol_table;
  32. class Object;
  33. struct Read_symbols_data;
  34. class Input_file_lib;
  35. class Incremental_archive_entry;
  36. // An entry in the archive map of offsets to members.
  37. struct Archive_member
  38. {
  39. Archive_member()
  40. : obj_(NULL), sd_(NULL), arg_serial_(0)
  41. { }
  42. Archive_member(Object* obj, Read_symbols_data* sd)
  43. : obj_(obj), sd_(sd), arg_serial_(0)
  44. { }
  45. // The object file.
  46. Object* obj_;
  47. // The data to pass from read_symbols() to add_symbols().
  48. Read_symbols_data* sd_;
  49. // The serial number of the file in the argument list.
  50. unsigned int arg_serial_;
  51. };
  52. // This class serves as a base class for Archive and Lib_group objects.
  53. class Library_base
  54. {
  55. public:
  56. Library_base(Task* task)
  57. : task_(task), incremental_info_(NULL)
  58. { }
  59. virtual
  60. ~Library_base()
  61. { }
  62. // The file name.
  63. const std::string&
  64. filename() const
  65. { return this->do_filename(); }
  66. // The modification time of the archive file.
  67. Timespec
  68. get_mtime()
  69. { return this->do_get_mtime(); }
  70. // When we see a symbol in an archive we might decide to include the member,
  71. // not include the member or be undecided. This enum represents these
  72. // possibilities.
  73. enum Should_include
  74. {
  75. SHOULD_INCLUDE_NO,
  76. SHOULD_INCLUDE_YES,
  77. SHOULD_INCLUDE_UNKNOWN
  78. };
  79. static Should_include
  80. should_include_member(Symbol_table* symtab, Layout*, const char* sym_name,
  81. Symbol** symp, std::string* why, char** tmpbufp,
  82. size_t* tmpbuflen);
  83. // Store a pointer to the incremental link info for the library.
  84. void
  85. set_incremental_info(Incremental_archive_entry* info)
  86. { this->incremental_info_ = info; }
  87. // Return the pointer to the incremental link info for the library.
  88. Incremental_archive_entry*
  89. incremental_info() const
  90. { return this->incremental_info_; }
  91. // Abstract base class for processing unused symbols.
  92. class Symbol_visitor_base
  93. {
  94. public:
  95. Symbol_visitor_base()
  96. { }
  97. virtual
  98. ~Symbol_visitor_base()
  99. { }
  100. // This function will be called for each unused global
  101. // symbol in a library, with a pointer to the symbol name.
  102. virtual void
  103. visit(const char* /* name */) = 0;
  104. };
  105. // Iterator for unused global symbols in the library.
  106. // Calls v->visit() for each global symbol defined
  107. // in each unused library member, passing a pointer to
  108. // the symbol name.
  109. void
  110. for_all_unused_symbols(Symbol_visitor_base* v) const
  111. { this->do_for_all_unused_symbols(v); }
  112. protected:
  113. // The task reading this archive.
  114. Task *task_;
  115. private:
  116. // The file name.
  117. virtual const std::string&
  118. do_filename() const = 0;
  119. // Return the modification time of the archive file.
  120. virtual Timespec
  121. do_get_mtime() = 0;
  122. // Iterator for unused global symbols in the library.
  123. virtual void
  124. do_for_all_unused_symbols(Symbol_visitor_base* v) const = 0;
  125. // The incremental link information for this archive.
  126. Incremental_archive_entry* incremental_info_;
  127. };
  128. // This class represents an archive--generally a libNAME.a file.
  129. // Archives have a symbol table and a list of objects.
  130. class Archive : public Library_base
  131. {
  132. public:
  133. Archive(const std::string& name, Input_file* input_file,
  134. bool is_thin_archive, Dirsearch* dirpath, Task* task);
  135. // The length of the magic string at the start of an archive.
  136. static const int sarmag = 8;
  137. // The magic string at the start of an archive.
  138. static const char armag[sarmag];
  139. static const char armagt[sarmag];
  140. // The string expected at the end of an archive member header.
  141. static const char arfmag[2];
  142. // The name of the object. This is the name used on the command
  143. // line; e.g., if "-lgcc" is on the command line, this will be
  144. // "gcc".
  145. const std::string&
  146. name() const
  147. { return this->name_; }
  148. // The input file.
  149. const Input_file*
  150. input_file() const
  151. { return this->input_file_; }
  152. // Set up the archive: read the symbol map.
  153. void
  154. setup();
  155. // Get a reference to the underlying file.
  156. File_read&
  157. file()
  158. { return this->input_file_->file(); }
  159. const File_read&
  160. file() const
  161. { return this->input_file_->file(); }
  162. // Lock the underlying file.
  163. void
  164. lock(const Task* t)
  165. { this->input_file_->file().lock(t); }
  166. // Unlock the underlying file.
  167. void
  168. unlock(const Task* t)
  169. { this->input_file_->file().unlock(t); }
  170. // Return whether the underlying file is locked.
  171. bool
  172. is_locked() const
  173. { return this->input_file_->file().is_locked(); }
  174. // Return the token, so that the task can be queued.
  175. Task_token*
  176. token()
  177. { return this->input_file_->file().token(); }
  178. // Release the underlying file.
  179. void
  180. release()
  181. { this->input_file_->file().release(); }
  182. // Clear uncached views in the underlying file.
  183. void
  184. clear_uncached_views()
  185. { this->input_file_->file().clear_uncached_views(); }
  186. // Whether this is a thin archive.
  187. bool
  188. is_thin_archive() const
  189. { return this->is_thin_archive_; }
  190. // Unlock any nested archives.
  191. void
  192. unlock_nested_archives();
  193. // Select members from the archive as needed and add them to the
  194. // link.
  195. bool
  196. add_symbols(Symbol_table*, Layout*, Input_objects*, Mapfile*);
  197. // Return whether the archive defines the symbol.
  198. bool
  199. defines_symbol(Symbol*) const;
  200. // Dump statistical information to stderr.
  201. static void
  202. print_stats();
  203. // Return the number of members in the archive.
  204. size_t
  205. count_members();
  206. // Return the no-export flag.
  207. bool
  208. no_export()
  209. { return this->no_export_; }
  210. private:
  211. Archive(const Archive&);
  212. Archive& operator=(const Archive&);
  213. // The file name.
  214. const std::string&
  215. do_filename() const
  216. { return this->input_file_->filename(); }
  217. // The modification time of the archive file.
  218. Timespec
  219. do_get_mtime()
  220. { return this->file().get_mtime(); }
  221. struct Archive_header;
  222. // Total number of archives seen.
  223. static unsigned int total_archives;
  224. // Total number of archive members seen.
  225. static unsigned int total_members;
  226. // Number of archive members loaded.
  227. static unsigned int total_members_loaded;
  228. // Get a view into the underlying file.
  229. const unsigned char*
  230. get_view(off_t start, section_size_type size, bool aligned, bool cache)
  231. { return this->input_file_->file().get_view(0, start, size, aligned, cache); }
  232. // Read the archive symbol map.
  233. void
  234. read_armap(off_t start, section_size_type size);
  235. // Read an archive member header at OFF. CACHE is whether to cache
  236. // the file view. Return the size of the member, and set *PNAME to
  237. // the name.
  238. off_t
  239. read_header(off_t off, bool cache, std::string* pname, off_t* nested_off);
  240. // Interpret an archive header HDR at OFF. Return the size of the
  241. // member, and set *PNAME to the name.
  242. off_t
  243. interpret_header(const Archive_header* hdr, off_t off, std::string* pname,
  244. off_t* nested_off) const;
  245. // Get the file and offset for an archive member, which may be an
  246. // external member of a thin archive. Set *INPUT_FILE to the
  247. // file containing the actual member, *MEMOFF to the offset
  248. // within that file (0 if not a nested archive), and *MEMBER_NAME
  249. // to the name of the archive member. Return TRUE on success.
  250. bool
  251. get_file_and_offset(off_t off, Input_file** input_file, off_t* memoff,
  252. off_t* memsize, std::string* member_name);
  253. // Return an ELF object for the member at offset OFF.
  254. Object*
  255. get_elf_object_for_member(off_t off, bool*);
  256. // Read the symbols from all the archive members in the link.
  257. void
  258. read_all_symbols();
  259. // Read the symbols from an archive member in the link. OFF is the file
  260. // offset of the member header.
  261. void
  262. read_symbols(off_t off);
  263. // Include all the archive members in the link.
  264. bool
  265. include_all_members(Symbol_table*, Layout*, Input_objects*, Mapfile*);
  266. // Include an archive member in the link.
  267. bool
  268. include_member(Symbol_table*, Layout*, Input_objects*, off_t off,
  269. Mapfile*, Symbol*, const char* why);
  270. // Return whether we found this archive by searching a directory.
  271. bool
  272. searched_for() const
  273. { return this->input_file_->will_search_for(); }
  274. // Iterate over archive members.
  275. class const_iterator;
  276. const_iterator
  277. begin();
  278. const_iterator
  279. end();
  280. friend class const_iterator;
  281. // Iterator for unused global symbols in the library.
  282. void
  283. do_for_all_unused_symbols(Symbol_visitor_base* v) const;
  284. // An entry in the archive map of symbols to object files.
  285. struct Armap_entry
  286. {
  287. // The offset to the symbol name in armap_names_.
  288. off_t name_offset;
  289. // The file offset to the object in the archive.
  290. off_t file_offset;
  291. };
  292. // A simple hash code for off_t values.
  293. class Seen_hash
  294. {
  295. public:
  296. size_t operator()(off_t val) const
  297. { return static_cast<size_t>(val); }
  298. };
  299. // For keeping track of open nested archives in a thin archive file.
  300. typedef Unordered_map<std::string, Archive*> Nested_archive_table;
  301. // Name of object as printed to user.
  302. std::string name_;
  303. // For reading the file.
  304. Input_file* input_file_;
  305. // The archive map.
  306. std::vector<Armap_entry> armap_;
  307. // The names in the archive map.
  308. std::string armap_names_;
  309. // The extended name table.
  310. std::string extended_names_;
  311. // Track which symbols in the archive map are for elements which are
  312. // defined or which have already been included in the link.
  313. std::vector<bool> armap_checked_;
  314. // Track which elements have been included by offset.
  315. Unordered_set<off_t, Seen_hash> seen_offsets_;
  316. // Table of objects whose symbols have been pre-read.
  317. std::map<off_t, Archive_member> members_;
  318. // True if this is a thin archive.
  319. const bool is_thin_archive_;
  320. // True if we have included at least one object from this archive.
  321. bool included_member_;
  322. // Table of nested archives, indexed by filename.
  323. Nested_archive_table nested_archives_;
  324. // The directory search path.
  325. Dirsearch* dirpath_;
  326. // Number of members in this archive;
  327. unsigned int num_members_;
  328. // True if we exclude this library archive from automatic export.
  329. bool no_export_;
  330. // True if this library has been included as a --whole-archive.
  331. bool included_all_members_;
  332. };
  333. // This class is used to read an archive and pick out the desired
  334. // elements and add them to the link.
  335. class Add_archive_symbols : public Task
  336. {
  337. public:
  338. Add_archive_symbols(Symbol_table* symtab, Layout* layout,
  339. Input_objects* input_objects, Dirsearch* dirpath,
  340. int dirindex, Mapfile* mapfile,
  341. const Input_argument* input_argument,
  342. Archive* archive, Input_group* input_group,
  343. Task_token* this_blocker,
  344. Task_token* next_blocker)
  345. : symtab_(symtab), layout_(layout), input_objects_(input_objects),
  346. dirpath_(dirpath), dirindex_(dirindex), mapfile_(mapfile),
  347. input_argument_(input_argument), archive_(archive),
  348. input_group_(input_group), this_blocker_(this_blocker),
  349. next_blocker_(next_blocker)
  350. { }
  351. ~Add_archive_symbols();
  352. // The standard Task methods.
  353. Task_token*
  354. is_runnable();
  355. void
  356. locks(Task_locker*);
  357. void
  358. run(Workqueue*);
  359. std::string
  360. get_name() const
  361. {
  362. if (this->archive_ == NULL)
  363. return "Add_archive_symbols";
  364. return "Add_archive_symbols " + this->archive_->file().filename();
  365. }
  366. private:
  367. Symbol_table* symtab_;
  368. Layout* layout_;
  369. Input_objects* input_objects_;
  370. Dirsearch* dirpath_;
  371. int dirindex_;
  372. Mapfile* mapfile_;
  373. const Input_argument* input_argument_;
  374. Archive* archive_;
  375. Input_group* input_group_;
  376. Task_token* this_blocker_;
  377. Task_token* next_blocker_;
  378. };
  379. // This class represents the files surrounded by a --start-lib ... --end-lib.
  380. class Lib_group : public Library_base
  381. {
  382. public:
  383. Lib_group(const Input_file_lib* lib, Task* task);
  384. // Select members from the lib group as needed and add them to the link.
  385. void
  386. add_symbols(Symbol_table*, Layout*, Input_objects*);
  387. // Include a member of the lib group in the link.
  388. void
  389. include_member(Symbol_table*, Layout*, Input_objects*, const Archive_member&);
  390. Archive_member*
  391. get_member(int i)
  392. {
  393. return &this->members_[i];
  394. }
  395. // Total number of archives seen.
  396. static unsigned int total_lib_groups;
  397. // Total number of archive members seen.
  398. static unsigned int total_members;
  399. // Number of archive members loaded.
  400. static unsigned int total_members_loaded;
  401. // Dump statistical information to stderr.
  402. static void
  403. print_stats();
  404. private:
  405. // The file name.
  406. const std::string&
  407. do_filename() const;
  408. // A Lib_group does not have a modification time, since there is no
  409. // real library file.
  410. Timespec
  411. do_get_mtime()
  412. { return Timespec(0, 0); }
  413. // Iterator for unused global symbols in the library.
  414. void
  415. do_for_all_unused_symbols(Symbol_visitor_base*) const;
  416. // Table of the objects in the group.
  417. std::vector<Archive_member> members_;
  418. };
  419. // This class is used to pick out the desired elements and add them to the link.
  420. class Add_lib_group_symbols : public Task
  421. {
  422. public:
  423. Add_lib_group_symbols(Symbol_table* symtab, Layout* layout,
  424. Input_objects* input_objects,
  425. Lib_group* lib, Task_token* next_blocker)
  426. : symtab_(symtab), layout_(layout), input_objects_(input_objects),
  427. lib_(lib), readsyms_blocker_(NULL), this_blocker_(NULL),
  428. next_blocker_(next_blocker)
  429. { }
  430. ~Add_lib_group_symbols();
  431. // The standard Task methods.
  432. Task_token*
  433. is_runnable();
  434. void
  435. locks(Task_locker*);
  436. void
  437. run(Workqueue*);
  438. // Set the blocker to use for this task.
  439. void
  440. set_blocker(Task_token* readsyms_blocker, Task_token* this_blocker)
  441. {
  442. gold_assert(this->readsyms_blocker_ == NULL && this->this_blocker_ == NULL);
  443. this->readsyms_blocker_ = readsyms_blocker;
  444. this->this_blocker_ = this_blocker;
  445. }
  446. std::string
  447. get_name() const
  448. {
  449. return "Add_lib_group_symbols";
  450. }
  451. private:
  452. Symbol_table* symtab_;
  453. Layout* layout_;
  454. Input_objects* input_objects_;
  455. Lib_group* lib_;
  456. Task_token* readsyms_blocker_;
  457. Task_token* this_blocker_;
  458. Task_token* next_blocker_;
  459. };
  460. } // End namespace gold.
  461. #endif // !defined(GOLD_ARCHIVE_H)