fileread.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. // fileread.h -- read files for gold -*- C++ -*-
  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. // Classes used to read data from binary input files.
  18. #ifndef GOLD_FILEREAD_H
  19. #define GOLD_FILEREAD_H
  20. #include <list>
  21. #include <map>
  22. #include <string>
  23. #include <vector>
  24. #include "token.h"
  25. namespace gold
  26. {
  27. // Since not all system supports stat.st_mtim and struct timespec,
  28. // we define our own structure and fill the nanoseconds if we can.
  29. struct Timespec
  30. {
  31. Timespec()
  32. : seconds(0), nanoseconds(0)
  33. { }
  34. Timespec(time_t a_seconds, int a_nanoseconds)
  35. : seconds(a_seconds), nanoseconds(a_nanoseconds)
  36. { }
  37. time_t seconds;
  38. int nanoseconds;
  39. };
  40. // Get the last modified time of an unopened file. Returns false if the
  41. // file does not exist.
  42. bool
  43. get_mtime(const char* filename, Timespec* mtime);
  44. class Position_dependent_options;
  45. class Input_file_argument;
  46. class Dirsearch;
  47. class File_view;
  48. // File_read manages a file descriptor and mappings for a file we are
  49. // reading.
  50. class File_read
  51. {
  52. public:
  53. File_read()
  54. : name_(), descriptor_(-1), is_descriptor_opened_(false), object_count_(0),
  55. size_(0), token_(false), views_(), saved_views_(), mapped_bytes_(0),
  56. released_(true), whole_file_view_(NULL)
  57. { }
  58. ~File_read();
  59. // Open a file.
  60. bool
  61. open(const Task*, const std::string& name);
  62. // Pretend to open the file, but provide the file contents. No
  63. // actual file system activity will occur. This is used for
  64. // testing.
  65. bool
  66. open(const Task*, const std::string& name, const unsigned char* contents,
  67. off_t size);
  68. // Return the file name.
  69. const std::string&
  70. filename() const
  71. { return this->name_; }
  72. // Add an object associated with a file.
  73. void
  74. add_object()
  75. { ++this->object_count_; }
  76. // Remove an object associated with a file.
  77. void
  78. remove_object()
  79. { --this->object_count_; }
  80. // Lock the file for exclusive access within a particular Task::run
  81. // execution. This routine may only be called when the workqueue
  82. // lock is held.
  83. void
  84. lock(const Task* t);
  85. // Unlock the file.
  86. void
  87. unlock(const Task* t);
  88. // Test whether the object is locked.
  89. bool
  90. is_locked() const;
  91. // Return the token, so that the task can be queued.
  92. Task_token*
  93. token()
  94. { return &this->token_; }
  95. // Release the file. This indicates that we aren't going to do
  96. // anything further with it until it is unlocked. This is used
  97. // because a Task which locks the file never calls either lock or
  98. // unlock; it just locks the token. The basic rule is that a Task
  99. // which locks a file via the Task::locks interface must explicitly
  100. // call release() when it is done. This is not necessary for code
  101. // which calls unlock() on the file.
  102. void
  103. release();
  104. // Return the size of the file.
  105. off_t
  106. filesize() const
  107. { return this->size_; }
  108. // Return a view into the file starting at file offset START for
  109. // SIZE bytes. OFFSET is the offset into the input file for the
  110. // file we are reading; this is zero for a normal object file,
  111. // non-zero for an object file in an archive. ALIGNED is true if
  112. // the data must be naturally aligned (i.e., aligned to the size
  113. // of a target word); this only matters when OFFSET is not zero.
  114. // The pointer will remain valid until the File_read is unlocked.
  115. // It is an error if we can not read enough data from the file.
  116. // The CACHE parameter is a hint as to whether it will be useful
  117. // to cache this data for later accesses--i.e., later calls to
  118. // get_view, read, or get_lasting_view which retrieve the same
  119. // data.
  120. const unsigned char*
  121. get_view(off_t offset, off_t start, section_size_type size, bool aligned,
  122. bool cache);
  123. // Read data from the file into the buffer P starting at file offset
  124. // START for SIZE bytes.
  125. void
  126. read(off_t start, section_size_type size, void* p);
  127. // Return a lasting view into the file starting at file offset START
  128. // for SIZE bytes. This is allocated with new, and the caller is
  129. // responsible for deleting it when done. The data associated with
  130. // this view will remain valid until the view is deleted. It is an
  131. // error if we can not read enough data from the file. The OFFSET,
  132. // ALIGNED and CACHE parameters are as in get_view.
  133. File_view*
  134. get_lasting_view(off_t offset, off_t start, section_size_type size,
  135. bool aligned, bool cache);
  136. // Mark all views as no longer cached.
  137. void
  138. clear_view_cache_marks();
  139. // Discard all uncached views. This is normally done by release(),
  140. // but not for objects in archives. FIXME: This is a complicated
  141. // interface, and it would be nice to have something more automatic.
  142. void
  143. clear_uncached_views()
  144. { this->clear_views(CLEAR_VIEWS_ARCHIVE); }
  145. // A struct used to do a multiple read.
  146. struct Read_multiple_entry
  147. {
  148. // The file offset of the data to read.
  149. off_t file_offset;
  150. // The amount of data to read.
  151. section_size_type size;
  152. // The buffer where the data should be placed.
  153. unsigned char* buffer;
  154. Read_multiple_entry(off_t o, section_size_type s, unsigned char* b)
  155. : file_offset(o), size(s), buffer(b)
  156. { }
  157. };
  158. typedef std::vector<Read_multiple_entry> Read_multiple;
  159. // Read a bunch of data from the file into various different
  160. // locations. The vector must be sorted by ascending file_offset.
  161. // BASE is a base offset to be added to all the offsets in the
  162. // vector.
  163. void
  164. read_multiple(off_t base, const Read_multiple&);
  165. // Dump statistical information to stderr.
  166. static void
  167. print_stats();
  168. // Write the dependency file listing all files read.
  169. static void
  170. write_dependency_file(const char* dependency_file_name,
  171. const char* output_file_name);
  172. // Record that a file was read. File_read::open does this.
  173. static void
  174. record_file_read(const std::string& name);
  175. // Return the open file descriptor (for plugins).
  176. int
  177. descriptor()
  178. {
  179. this->reopen_descriptor();
  180. return this->descriptor_;
  181. }
  182. // Return the file last modification time. Calls gold_fatal if the stat
  183. // system call failed.
  184. Timespec
  185. get_mtime();
  186. private:
  187. // Control for what views to clear.
  188. enum Clear_views_mode
  189. {
  190. // Clear uncached views not used by an archive.
  191. CLEAR_VIEWS_NORMAL,
  192. // Clear all uncached views (including in an archive).
  193. CLEAR_VIEWS_ARCHIVE,
  194. // Clear all views (i.e., we're destroying the file).
  195. CLEAR_VIEWS_ALL
  196. };
  197. // This class may not be copied.
  198. File_read(const File_read&);
  199. File_read& operator=(const File_read&);
  200. // Total bytes mapped into memory during the link if --stats.
  201. static unsigned long long total_mapped_bytes;
  202. // Current number of bytes mapped into memory during the link if
  203. // --stats.
  204. static unsigned long long current_mapped_bytes;
  205. // High water mark of bytes mapped into memory during the link if
  206. // --stats.
  207. static unsigned long long maximum_mapped_bytes;
  208. // Set of names of all files read.
  209. static std::vector<std::string> files_read;
  210. // A view into the file.
  211. class View
  212. {
  213. public:
  214. // Specifies how to dispose the data on destruction of the view.
  215. enum Data_ownership
  216. {
  217. // Data owned by File object - nothing done in destructor.
  218. DATA_NOT_OWNED,
  219. // Data allocated with new[] and owned by this object - should
  220. // use delete[].
  221. DATA_ALLOCATED_ARRAY,
  222. // Data mmapped and owned by this object - should munmap.
  223. DATA_MMAPPED
  224. };
  225. View(off_t start, section_size_type size, const unsigned char* data,
  226. unsigned int byteshift, bool cache, Data_ownership data_ownership)
  227. : start_(start), size_(size), data_(data), lock_count_(0),
  228. byteshift_(byteshift), cache_(cache), data_ownership_(data_ownership),
  229. accessed_(true)
  230. { }
  231. ~View();
  232. off_t
  233. start() const
  234. { return this->start_; }
  235. section_size_type
  236. size() const
  237. { return this->size_; }
  238. const unsigned char*
  239. data() const
  240. { return this->data_; }
  241. void
  242. lock();
  243. void
  244. unlock();
  245. bool
  246. is_locked();
  247. unsigned int
  248. byteshift() const
  249. { return this->byteshift_; }
  250. void
  251. set_cache()
  252. { this->cache_ = true; }
  253. void
  254. clear_cache()
  255. { this->cache_ = false; }
  256. bool
  257. should_cache() const
  258. { return this->cache_; }
  259. void
  260. set_accessed()
  261. { this->accessed_ = true; }
  262. void
  263. clear_accessed()
  264. { this->accessed_= false; }
  265. bool
  266. accessed() const
  267. { return this->accessed_; }
  268. // Returns TRUE if this view contains permanent data -- e.g., data that
  269. // was supplied by the owner of the File object.
  270. bool
  271. is_permanent_view() const
  272. { return this->data_ownership_ == DATA_NOT_OWNED; }
  273. private:
  274. View(const View&);
  275. View& operator=(const View&);
  276. // The file offset of the start of the view.
  277. off_t start_;
  278. // The size of the view.
  279. section_size_type size_;
  280. // A pointer to the actual bytes.
  281. const unsigned char* data_;
  282. // The number of locks on this view.
  283. int lock_count_;
  284. // The number of bytes that the view is shifted relative to the
  285. // underlying file. This is used to align data. This is normally
  286. // zero, except possibly for an object in an archive.
  287. unsigned int byteshift_;
  288. // Whether the view is cached.
  289. bool cache_;
  290. // Whether the view is mapped into memory. If not, data_ points
  291. // to memory allocated using new[].
  292. Data_ownership data_ownership_;
  293. // Whether the view has been accessed recently.
  294. bool accessed_;
  295. };
  296. friend class View;
  297. friend class File_view;
  298. // The type of a mapping from page start and byte shift to views.
  299. typedef std::map<std::pair<off_t, unsigned int>, View*> Views;
  300. // A simple list of Views.
  301. typedef std::list<View*> Saved_views;
  302. // Open the descriptor if necessary.
  303. void
  304. reopen_descriptor();
  305. // Find a view into the file.
  306. View*
  307. find_view(off_t start, section_size_type size, unsigned int byteshift,
  308. View** vshifted) const;
  309. // Read data from the file into a buffer.
  310. void
  311. do_read(off_t start, section_size_type size, void* p);
  312. // Add a view.
  313. void
  314. add_view(View*);
  315. // Make a view into the file.
  316. View*
  317. make_view(off_t start, section_size_type size, unsigned int byteshift,
  318. bool cache);
  319. // Find or make a view into the file.
  320. View*
  321. find_or_make_view(off_t offset, off_t start, section_size_type size,
  322. bool aligned, bool cache);
  323. // Clear the file views.
  324. void
  325. clear_views(Clear_views_mode);
  326. // The size of a file page for buffering data.
  327. static const off_t page_size = 8192;
  328. // Given a file offset, return the page offset.
  329. static off_t
  330. page_offset(off_t file_offset)
  331. { return file_offset & ~ (page_size - 1); }
  332. // Given a file size, return the size to read integral pages.
  333. static off_t
  334. pages(off_t file_size)
  335. { return (file_size + (page_size - 1)) & ~ (page_size - 1); }
  336. // The maximum number of entries we will pass to ::readv.
  337. static const size_t max_readv_entries = 128;
  338. // Use readv to read data.
  339. void
  340. do_readv(off_t base, const Read_multiple&, size_t start, size_t count);
  341. // File name.
  342. std::string name_;
  343. // File descriptor.
  344. int descriptor_;
  345. // Whether we have regained the descriptor after releasing the file.
  346. bool is_descriptor_opened_;
  347. // The number of objects associated with this file. This will be
  348. // more than 1 in the case of an archive.
  349. int object_count_;
  350. // File size.
  351. off_t size_;
  352. // A token used to lock the file.
  353. Task_token token_;
  354. // Buffered views into the file.
  355. Views views_;
  356. // List of views which were locked but had to be removed from views_
  357. // because they were not large enough.
  358. Saved_views saved_views_;
  359. // Total amount of space mapped into memory. This is only changed
  360. // while the file is locked. When we unlock the file, we transfer
  361. // the total to total_mapped_bytes, and reset this to zero.
  362. size_t mapped_bytes_;
  363. // Whether the file was released.
  364. bool released_;
  365. // A view containing the whole file. May be NULL if we mmap only
  366. // the relevant parts of the file. Not NULL if:
  367. // - Flag --mmap_whole_files is set (default on 64-bit hosts).
  368. // - The contents was specified in the constructor. Used only for
  369. // testing purposes).
  370. View* whole_file_view_;
  371. };
  372. // A view of file data that persists even when the file is unlocked.
  373. // Callers should destroy these when no longer required. These are
  374. // obtained form File_read::get_lasting_view. They may only be
  375. // destroyed when the underlying File_read is locked.
  376. class File_view
  377. {
  378. public:
  379. // This may only be called when the underlying File_read is locked.
  380. ~File_view();
  381. // Return a pointer to the data associated with this view.
  382. const unsigned char*
  383. data() const
  384. { return this->data_; }
  385. private:
  386. File_view(const File_view&);
  387. File_view& operator=(const File_view&);
  388. friend class File_read;
  389. // Callers have to get these via File_read::get_lasting_view.
  390. File_view(File_read& file, File_read::View* view, const unsigned char* data)
  391. : file_(file), view_(view), data_(data)
  392. { }
  393. File_read& file_;
  394. File_read::View* view_;
  395. const unsigned char* data_;
  396. };
  397. // All the information we hold for a single input file. This can be
  398. // an object file, a shared library, or an archive.
  399. class Input_file
  400. {
  401. public:
  402. enum Format
  403. {
  404. FORMAT_NONE,
  405. FORMAT_ELF,
  406. FORMAT_BINARY
  407. };
  408. Input_file(const Input_file_argument* input_argument)
  409. : input_argument_(input_argument), found_name_(), file_(),
  410. is_in_sysroot_(false), format_(FORMAT_NONE)
  411. { }
  412. // Create an input file given just a filename.
  413. Input_file(const char* name);
  414. // Create an input file with the contents already provided. This is
  415. // only used for testing. With this path, don't call the open
  416. // method.
  417. Input_file(const Task*, const char* name, const unsigned char* contents,
  418. off_t size);
  419. // Return the command line argument.
  420. const Input_file_argument*
  421. input_file_argument() const
  422. { return this->input_argument_; }
  423. // Return whether this is a file that we will search for in the list
  424. // of directories.
  425. bool
  426. will_search_for() const;
  427. // Open the file. If the open fails, this will report an error and
  428. // return false. If there is a search, it starts at directory
  429. // *PINDEX. *PINDEX should be initialized to zero. It may be
  430. // restarted to find the next file with a matching name by
  431. // incrementing the result and calling this again.
  432. bool
  433. open(const Dirsearch&, const Task*, int* pindex);
  434. // Return the name given by the user. For -lc this will return "c".
  435. const char*
  436. name() const;
  437. // Return the file name. For -lc this will return something like
  438. // "/usr/lib/libc.so".
  439. const std::string&
  440. filename() const
  441. { return this->file_.filename(); }
  442. // Return the name under which we found the file, corresponding to
  443. // the command line. For -lc this will return something like
  444. // "libc.so".
  445. const std::string&
  446. found_name() const
  447. { return this->found_name_; }
  448. // Return the position dependent options.
  449. const Position_dependent_options&
  450. options() const;
  451. // Return the file.
  452. File_read&
  453. file()
  454. { return this->file_; }
  455. const File_read&
  456. file() const
  457. { return this->file_; }
  458. // Whether we found the file in a directory in the system root.
  459. bool
  460. is_in_sysroot() const
  461. { return this->is_in_sysroot_; }
  462. // Whether this file is in a system directory.
  463. bool
  464. is_in_system_directory() const;
  465. // Return whether this file is to be read only for its symbols.
  466. bool
  467. just_symbols() const;
  468. // Return the format of the unconverted input file.
  469. Format
  470. format() const
  471. { return this->format_; }
  472. // Try to find a file in the extra search dirs. Returns true on success.
  473. static bool
  474. try_extra_search_path(int* pindex,
  475. const Input_file_argument* input_argument,
  476. std::string filename, std::string* found_name,
  477. std::string* namep);
  478. // Find the actual file.
  479. static bool
  480. find_file(const Dirsearch& dirpath, int* pindex,
  481. const Input_file_argument* input_argument,
  482. bool* is_in_sysroot,
  483. std::string* found_name, std::string* namep);
  484. private:
  485. Input_file(const Input_file&);
  486. Input_file& operator=(const Input_file&);
  487. // Open a binary file.
  488. bool
  489. open_binary(const Task* task, const std::string& name);
  490. // The argument from the command line.
  491. const Input_file_argument* input_argument_;
  492. // The name under which we opened the file. This is like the name
  493. // on the command line, but -lc turns into libc.so (or whatever).
  494. // It only includes the full path if the path was on the command
  495. // line.
  496. std::string found_name_;
  497. // The file after we open it.
  498. File_read file_;
  499. // Whether we found the file in a directory in the system root.
  500. bool is_in_sysroot_;
  501. // Format of unconverted input file.
  502. Format format_;
  503. };
  504. } // end namespace gold
  505. #endif // !defined(GOLD_FILEREAD_H)