OSDN Git Service

* mapfile.cc: New file.
[pf3gnuchains/pf3gnuchains3x.git] / gold / archive.h
1 // archive.h -- archive support for gold      -*- C++ -*-
2
3 // Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
5
6 // This file is part of gold.
7
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
12
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
22
23 #ifndef GOLD_ARCHIVE_H
24 #define GOLD_ARCHIVE_H
25
26 #include <string>
27 #include <vector>
28
29 #include "fileread.h"
30 #include "workqueue.h"
31
32 namespace gold
33 {
34
35 class Task;
36 class Input_file;
37 class Input_objects;
38 class Input_group;
39 class Layout;
40 class Symbol_table;
41
42 // This class represents an archive--generally a libNAME.a file.
43 // Archives have a symbol table and a list of objects.
44
45 class Archive
46 {
47  public:
48   Archive(const std::string& name, Input_file* input_file,
49           bool is_thin_archive, Dirsearch* dirpath, Task* task)
50     : name_(name), input_file_(input_file), armap_(), armap_names_(),
51       extended_names_(), armap_checked_(), seen_offsets_(),
52       is_thin_archive_(is_thin_archive), dirpath_(dirpath), task_(task)
53   { }
54
55   // The length of the magic string at the start of an archive.
56   static const int sarmag = 8;
57
58   // The magic string at the start of an archive.
59   static const char armag[sarmag];
60   static const char armagt[sarmag];
61
62   // The string expected at the end of an archive member header.
63   static const char arfmag[2];
64
65   // The name of the object.
66   const std::string&
67   name() const
68   { return this->name_; }
69
70   // Set up the archive: read the symbol map.
71   void
72   setup();
73
74   // Get a reference to the underlying file.
75   File_read&
76   file()
77   { return this->input_file_->file(); }
78
79   const File_read&
80   file() const
81   { return this->input_file_->file(); }
82
83   // Lock the underlying file.
84   void
85   lock(const Task* t)
86   { this->input_file_->file().lock(t); }
87
88   // Unlock the underlying file.
89   void
90   unlock(const Task* t)
91   { this->input_file_->file().unlock(t); }
92
93   // Return whether the underlying file is locked.
94   bool
95   is_locked() const
96   { return this->input_file_->file().is_locked(); }
97
98   // Return the token, so that the task can be queued.
99   Task_token*
100   token()
101   { return this->input_file_->file().token(); }
102
103   // Release the underlying file.
104   void
105   release()
106   { this->input_file_->file().release(); }
107
108   // Clear uncached views in the underlying file.
109   void
110   clear_uncached_views()
111   { this->input_file_->file().clear_uncached_views(); }
112
113   // Unlock any nested archives.
114   void
115   unlock_nested_archives();
116
117   // Select members from the archive as needed and add them to the
118   // link.
119   void
120   add_symbols(Symbol_table*, Layout*, Input_objects*, Mapfile*);
121
122  private:
123   Archive(const Archive&);
124   Archive& operator=(const Archive&);
125
126   struct Archive_header;
127
128   // Get a view into the underlying file.
129   const unsigned char*
130   get_view(off_t start, section_size_type size, bool aligned, bool cache)
131   { return this->input_file_->file().get_view(0, start, size, aligned, cache); }
132
133   // Read the archive symbol map.
134   void
135   read_armap(off_t start, section_size_type size);
136
137   // Read an archive member header at OFF.  CACHE is whether to cache
138   // the file view.  Return the size of the member, and set *PNAME to
139   // the name.
140   off_t
141   read_header(off_t off, bool cache, std::string* pname, off_t* nested_off);
142
143   // Interpret an archive header HDR at OFF.  Return the size of the
144   // member, and set *PNAME to the name.
145   off_t
146   interpret_header(const Archive_header* hdr, off_t off, std::string* pname,
147                    off_t* nested_off);
148
149   // Include all the archive members in the link.
150   void
151   include_all_members(Symbol_table*, Layout*, Input_objects*, Mapfile*);
152
153   // Include an archive member in the link.
154   void
155   include_member(Symbol_table*, Layout*, Input_objects*, off_t off,
156                  Mapfile*, Symbol*, const char* why);
157
158   // An entry in the archive map of symbols to object files.
159   struct Armap_entry
160   {
161     // The offset to the symbol name in armap_names_.
162     off_t name_offset;
163     // The file offset to the object in the archive.
164     off_t file_offset;
165   };
166
167   // A simple hash code for off_t values.
168   class Seen_hash
169   {
170    public:
171     size_t operator()(off_t val) const
172     { return static_cast<size_t>(val); }
173   };
174
175   // For keeping track of open nested archives in a thin archive file.
176   typedef Unordered_map<std::string, Archive*> Nested_archive_table;
177
178   // Name of object as printed to user.
179   std::string name_;
180   // For reading the file.
181   Input_file* input_file_;
182   // The archive map.
183   std::vector<Armap_entry> armap_;
184   // The names in the archive map.
185   std::string armap_names_;
186   // The extended name table.
187   std::string extended_names_;
188   // Track which symbols in the archive map are for elements which are
189   // defined or which have already been included in the link.
190   std::vector<bool> armap_checked_;
191   // Track which elements have been included by offset.
192   Unordered_set<off_t, Seen_hash> seen_offsets_;
193   // True if this is a thin archive.
194   const bool is_thin_archive_;
195   // Table of nested archives, indexed by filename.
196   Nested_archive_table nested_archives_;
197   // The directory search path.
198   Dirsearch* dirpath_;
199   // The task reading this archive.
200   Task *task_;
201 };
202
203 // This class is used to read an archive and pick out the desired
204 // elements and add them to the link.
205
206 class Add_archive_symbols : public Task
207 {
208  public:
209   Add_archive_symbols(Symbol_table* symtab, Layout* layout,
210                       Input_objects* input_objects, Mapfile* mapfile,
211                       Archive* archive, Input_group* input_group,
212                       Task_token* this_blocker,
213                       Task_token* next_blocker)
214     : symtab_(symtab), layout_(layout), input_objects_(input_objects),
215       mapfile_(mapfile), archive_(archive), input_group_(input_group),
216       this_blocker_(this_blocker), next_blocker_(next_blocker)
217   { }
218
219   ~Add_archive_symbols();
220
221   // The standard Task methods.
222
223   Task_token*
224   is_runnable();
225
226   void
227   locks(Task_locker*);
228
229   void
230   run(Workqueue*);
231
232   std::string
233   get_name() const
234   {
235     if (this->archive_ == NULL)
236       return "Add_archive_symbols";
237     return "Add_archive_symbols " + this->archive_->file().filename();
238   }
239
240  private:
241   Symbol_table* symtab_;
242   Layout* layout_;
243   Input_objects* input_objects_;
244   Mapfile* mapfile_;
245   Archive* archive_;
246   Input_group* input_group_;
247   Task_token* this_blocker_;
248   Task_token* next_blocker_;
249 };
250
251 } // End namespace gold.
252
253 #endif // !defined(GOLD_ARCHIVE_H)