fileread.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. // fileread.h -- read files 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. // 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. // Return the open file descriptor (for plugins).
  169. int
  170. descriptor()
  171. {
  172. this->reopen_descriptor();
  173. return this->descriptor_;
  174. }
  175. // Return the file last modification time. Calls gold_fatal if the stat
  176. // system call failed.
  177. Timespec
  178. get_mtime();
  179. private:
  180. // Control for what views to clear.
  181. enum Clear_views_mode
  182. {
  183. // Clear uncached views not used by an archive.
  184. CLEAR_VIEWS_NORMAL,
  185. // Clear all uncached views (including in an archive).
  186. CLEAR_VIEWS_ARCHIVE,
  187. // Clear all views (i.e., we're destroying the file).
  188. CLEAR_VIEWS_ALL
  189. };
  190. // This class may not be copied.
  191. File_read(const File_read&);
  192. File_read& operator=(const File_read&);
  193. // Total bytes mapped into memory during the link if --stats.
  194. static unsigned long long total_mapped_bytes;
  195. // Current number of bytes mapped into memory during the link if
  196. // --stats.
  197. static unsigned long long current_mapped_bytes;
  198. // High water mark of bytes mapped into memory during the link if
  199. // --stats.
  200. static unsigned long long maximum_mapped_bytes;
  201. // A view into the file.
  202. class View
  203. {
  204. public:
  205. // Specifies how to dispose the data on destruction of the view.
  206. enum Data_ownership
  207. {
  208. // Data owned by File object - nothing done in destructor.
  209. DATA_NOT_OWNED,
  210. // Data allocated with new[] and owned by this object - should
  211. // use delete[].
  212. DATA_ALLOCATED_ARRAY,
  213. // Data mmapped and owned by this object - should munmap.
  214. DATA_MMAPPED
  215. };
  216. View(off_t start, section_size_type size, const unsigned char* data,
  217. unsigned int byteshift, bool cache, Data_ownership data_ownership)
  218. : start_(start), size_(size), data_(data), lock_count_(0),
  219. byteshift_(byteshift), cache_(cache), data_ownership_(data_ownership),
  220. accessed_(true)
  221. { }
  222. ~View();
  223. off_t
  224. start() const
  225. { return this->start_; }
  226. section_size_type
  227. size() const
  228. { return this->size_; }
  229. const unsigned char*
  230. data() const
  231. { return this->data_; }
  232. void
  233. lock();
  234. void
  235. unlock();
  236. bool
  237. is_locked();
  238. unsigned int
  239. byteshift() const
  240. { return this->byteshift_; }
  241. void
  242. set_cache()
  243. { this->cache_ = true; }
  244. void
  245. clear_cache()
  246. { this->cache_ = false; }
  247. bool
  248. should_cache() const
  249. { return this->cache_; }
  250. void
  251. set_accessed()
  252. { this->accessed_ = true; }
  253. void
  254. clear_accessed()
  255. { this->accessed_= false; }
  256. bool
  257. accessed() const
  258. { return this->accessed_; }
  259. // Returns TRUE if this view contains permanent data -- e.g., data that
  260. // was supplied by the owner of the File object.
  261. bool
  262. is_permanent_view() const
  263. { return this->data_ownership_ == DATA_NOT_OWNED; }
  264. private:
  265. View(const View&);
  266. View& operator=(const View&);
  267. // The file offset of the start of the view.
  268. off_t start_;
  269. // The size of the view.
  270. section_size_type size_;
  271. // A pointer to the actual bytes.
  272. const unsigned char* data_;
  273. // The number of locks on this view.
  274. int lock_count_;
  275. // The number of bytes that the view is shifted relative to the
  276. // underlying file. This is used to align data. This is normally
  277. // zero, except possibly for an object in an archive.
  278. unsigned int byteshift_;
  279. // Whether the view is cached.
  280. bool cache_;
  281. // Whether the view is mapped into memory. If not, data_ points
  282. // to memory allocated using new[].
  283. Data_ownership data_ownership_;
  284. // Whether the view has been accessed recently.
  285. bool accessed_;
  286. };
  287. friend class View;
  288. friend class File_view;
  289. // The type of a mapping from page start and byte shift to views.
  290. typedef std::map<std::pair<off_t, unsigned int>, View*> Views;
  291. // A simple list of Views.
  292. typedef std::list<View*> Saved_views;
  293. // Open the descriptor if necessary.
  294. void
  295. reopen_descriptor();
  296. // Find a view into the file.
  297. View*
  298. find_view(off_t start, section_size_type size, unsigned int byteshift,
  299. View** vshifted) const;
  300. // Read data from the file into a buffer.
  301. void
  302. do_read(off_t start, section_size_type size, void* p);
  303. // Add a view.
  304. void
  305. add_view(View*);
  306. // Make a view into the file.
  307. View*
  308. make_view(off_t start, section_size_type size, unsigned int byteshift,
  309. bool cache);
  310. // Find or make a view into the file.
  311. View*
  312. find_or_make_view(off_t offset, off_t start, section_size_type size,
  313. bool aligned, bool cache);
  314. // Clear the file views.
  315. void
  316. clear_views(Clear_views_mode);
  317. // The size of a file page for buffering data.
  318. static const off_t page_size = 8192;
  319. // Given a file offset, return the page offset.
  320. static off_t
  321. page_offset(off_t file_offset)
  322. { return file_offset & ~ (page_size - 1); }
  323. // Given a file size, return the size to read integral pages.
  324. static off_t
  325. pages(off_t file_size)
  326. { return (file_size + (page_size - 1)) & ~ (page_size - 1); }
  327. // The maximum number of entries we will pass to ::readv.
  328. static const size_t max_readv_entries = 128;
  329. // Use readv to read data.
  330. void
  331. do_readv(off_t base, const Read_multiple&, size_t start, size_t count);
  332. // File name.
  333. std::string name_;
  334. // File descriptor.
  335. int descriptor_;
  336. // Whether we have regained the descriptor after releasing the file.
  337. bool is_descriptor_opened_;
  338. // The number of objects associated with this file. This will be
  339. // more than 1 in the case of an archive.
  340. int object_count_;
  341. // File size.
  342. off_t size_;
  343. // A token used to lock the file.
  344. Task_token token_;
  345. // Buffered views into the file.
  346. Views views_;
  347. // List of views which were locked but had to be removed from views_
  348. // because they were not large enough.
  349. Saved_views saved_views_;
  350. // Total amount of space mapped into memory. This is only changed
  351. // while the file is locked. When we unlock the file, we transfer
  352. // the total to total_mapped_bytes, and reset this to zero.
  353. size_t mapped_bytes_;
  354. // Whether the file was released.
  355. bool released_;
  356. // A view containing the whole file. May be NULL if we mmap only
  357. // the relevant parts of the file. Not NULL if:
  358. // - Flag --mmap_whole_files is set (default on 64-bit hosts).
  359. // - The contents was specified in the constructor. Used only for
  360. // testing purposes).
  361. View* whole_file_view_;
  362. };
  363. // A view of file data that persists even when the file is unlocked.
  364. // Callers should destroy these when no longer required. These are
  365. // obtained form File_read::get_lasting_view. They may only be
  366. // destroyed when the underlying File_read is locked.
  367. class File_view
  368. {
  369. public:
  370. // This may only be called when the underlying File_read is locked.
  371. ~File_view();
  372. // Return a pointer to the data associated with this view.
  373. const unsigned char*
  374. data() const
  375. { return this->data_; }
  376. private:
  377. File_view(const File_view&);
  378. File_view& operator=(const File_view&);
  379. friend class File_read;
  380. // Callers have to get these via File_read::get_lasting_view.
  381. File_view(File_read& file, File_read::View* view, const unsigned char* data)
  382. : file_(file), view_(view), data_(data)
  383. { }
  384. File_read& file_;
  385. File_read::View* view_;
  386. const unsigned char* data_;
  387. };
  388. // All the information we hold for a single input file. This can be
  389. // an object file, a shared library, or an archive.
  390. class Input_file
  391. {
  392. public:
  393. enum Format
  394. {
  395. FORMAT_NONE,
  396. FORMAT_ELF,
  397. FORMAT_BINARY
  398. };
  399. Input_file(const Input_file_argument* input_argument)
  400. : input_argument_(input_argument), found_name_(), file_(),
  401. is_in_sysroot_(false), format_(FORMAT_NONE)
  402. { }
  403. // Create an input file given just a filename.
  404. Input_file(const char* name);
  405. // Create an input file with the contents already provided. This is
  406. // only used for testing. With this path, don't call the open
  407. // method.
  408. Input_file(const Task*, const char* name, const unsigned char* contents,
  409. off_t size);
  410. // Return the command line argument.
  411. const Input_file_argument*
  412. input_file_argument() const
  413. { return this->input_argument_; }
  414. // Return whether this is a file that we will search for in the list
  415. // of directories.
  416. bool
  417. will_search_for() const;
  418. // Open the file. If the open fails, this will report an error and
  419. // return false. If there is a search, it starts at directory
  420. // *PINDEX. *PINDEX should be initialized to zero. It may be
  421. // restarted to find the next file with a matching name by
  422. // incrementing the result and calling this again.
  423. bool
  424. open(const Dirsearch&, const Task*, int* pindex);
  425. // Return the name given by the user. For -lc this will return "c".
  426. const char*
  427. name() const;
  428. // Return the file name. For -lc this will return something like
  429. // "/usr/lib/libc.so".
  430. const std::string&
  431. filename() const
  432. { return this->file_.filename(); }
  433. // Return the name under which we found the file, corresponding to
  434. // the command line. For -lc this will return something like
  435. // "libc.so".
  436. const std::string&
  437. found_name() const
  438. { return this->found_name_; }
  439. // Return the position dependent options.
  440. const Position_dependent_options&
  441. options() const;
  442. // Return the file.
  443. File_read&
  444. file()
  445. { return this->file_; }
  446. const File_read&
  447. file() const
  448. { return this->file_; }
  449. // Whether we found the file in a directory in the system root.
  450. bool
  451. is_in_sysroot() const
  452. { return this->is_in_sysroot_; }
  453. // Whether this file is in a system directory.
  454. bool
  455. is_in_system_directory() const;
  456. // Return whether this file is to be read only for its symbols.
  457. bool
  458. just_symbols() const;
  459. // Return the format of the unconverted input file.
  460. Format
  461. format() const
  462. { return this->format_; }
  463. // Try to find a file in the extra search dirs. Returns true on success.
  464. static bool
  465. try_extra_search_path(int* pindex,
  466. const Input_file_argument* input_argument,
  467. std::string filename, std::string* found_name,
  468. std::string* namep);
  469. // Find the actual file.
  470. static bool
  471. find_file(const Dirsearch& dirpath, int* pindex,
  472. const Input_file_argument* input_argument,
  473. bool* is_in_sysroot,
  474. std::string* found_name, std::string* namep);
  475. private:
  476. Input_file(const Input_file&);
  477. Input_file& operator=(const Input_file&);
  478. // Open a binary file.
  479. bool
  480. open_binary(const Task* task, const std::string& name);
  481. // The argument from the command line.
  482. const Input_file_argument* input_argument_;
  483. // The name under which we opened the file. This is like the name
  484. // on the command line, but -lc turns into libc.so (or whatever).
  485. // It only includes the full path if the path was on the command
  486. // line.
  487. std::string found_name_;
  488. // The file after we open it.
  489. File_read file_;
  490. // Whether we found the file in a directory in the system root.
  491. bool is_in_sysroot_;
  492. // Format of unconverted input file.
  493. Format format_;
  494. };
  495. } // end namespace gold
  496. #endif // !defined(GOLD_FILEREAD_H)