]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gold/archive.h
Add global parameters.
[thirdparty/binutils-gdb.git] / gold / archive.h
1 // archive.h -- archive support for gold -*- C++ -*-
2
3 #ifndef GOLD_ARCHIVE_H
4 #define GOLD_ARCHIVE_H
5
6 #include <string>
7 #include <vector>
8
9 #include "workqueue.h"
10
11 namespace gold
12 {
13
14 class Input_file;
15 class Input_objects;
16 class Input_group;
17 class Layout;
18 class Symbol_table;
19
20 // This class represents an archive--generally a libNAME.a file.
21 // Archives have a symbol table and a list of objects.
22
23 class Archive
24 {
25 public:
26 Archive(const std::string& name, Input_file* input_file)
27 : name_(name), input_file_(input_file), armap_(), extended_names_()
28 { }
29
30 // The length of the magic string at the start of an archive.
31 static const int sarmag = 8;
32
33 // The magic string at the start of an archive.
34 static const char armag[sarmag];
35
36 // The string expected at the end of an archive member header.
37 static const char arfmag[2];
38
39 // The name of the object.
40 const std::string&
41 name() const
42 { return this->name_; }
43
44 // Set up the archive: read the symbol map.
45 void
46 setup();
47
48 // Get a reference to the underlying file.
49 File_read&
50 file()
51 { return this->input_file_->file(); }
52
53 // Lock the underlying file.
54 void
55 lock()
56 { this->input_file_->file().lock(); }
57
58 // Unlock the underlying file.
59 void
60 unlock()
61 { this->input_file_->file().unlock(); }
62
63 // Return whether the underlying file is locked.
64 bool
65 is_locked() const
66 { return this->input_file_->file().is_locked(); }
67
68 // Select members from the archive as needed and add them to the
69 // link.
70 void
71 add_symbols(Symbol_table*, Layout*, Input_objects*);
72
73 private:
74 Archive(const Archive&);
75 Archive& operator=(const Archive&);
76
77 struct Archive_header;
78
79 // Get a view into the underlying file.
80 const unsigned char*
81 get_view(off_t start, off_t size, off_t* pbytes = NULL)
82 { return this->input_file_->file().get_view(start, size, pbytes); }
83
84 // Read the archive symbol map.
85 void
86 read_armap(off_t start, off_t size);
87
88 // Read an archive member header at OFF. Return the size of the
89 // member, and set *PNAME to the name.
90 off_t
91 read_header(off_t off, std::string* pname);
92
93 // Interpret an archive header HDR at OFF. Return the size of the
94 // member, and set *PNAME to the name.
95 off_t
96 interpret_header(const Archive_header* hdr, off_t off, std::string* pname);
97
98 // Include all the archive members in the link.
99 void
100 include_all_members(Symbol_table*, Layout*, Input_objects*);
101
102 // Include an archive member in the link.
103 void
104 include_member(Symbol_table*, Layout*, Input_objects*, off_t off);
105
106 // An entry in the archive map of symbols to object files.
107 struct Armap_entry
108 {
109 // The symbol name.
110 const char* name;
111 // The offset to the file.
112 off_t offset;
113 };
114
115 // A simple hash code for off_t values.
116 class Seen_hash
117 {
118 public:
119 size_t operator()(off_t val) const
120 { return static_cast<size_t>(val); }
121 };
122
123 // Name of object as printed to user.
124 std::string name_;
125 // For reading the file.
126 Input_file* input_file_;
127 // The archive map.
128 std::vector<Armap_entry> armap_;
129 // The extended name table.
130 std::string extended_names_;
131 // Track which symbols in the archive map are for elements which are
132 // defined or which have already been included in the link.
133 std::vector<bool> armap_checked_;
134 // Track which elements have been included by offset.
135 Unordered_set<off_t, Seen_hash> seen_offsets_;
136 };
137
138 // This class is used to read an archive and pick out the desired
139 // elements and add them to the link.
140
141 class Add_archive_symbols : public Task
142 {
143 public:
144 Add_archive_symbols(Symbol_table* symtab, Layout* layout,
145 Input_objects* input_objects,
146 Archive* archive, Input_group* input_group,
147 Task_token* this_blocker,
148 Task_token* next_blocker)
149 : symtab_(symtab), layout_(layout), input_objects_(input_objects),
150 archive_(archive), input_group_(input_group),
151 this_blocker_(this_blocker), next_blocker_(next_blocker)
152 { }
153
154 ~Add_archive_symbols();
155
156 // The standard Task methods.
157
158 Is_runnable_type
159 is_runnable(Workqueue*);
160
161 Task_locker*
162 locks(Workqueue*);
163
164 void
165 run(Workqueue*);
166
167 private:
168 class Add_archive_symbols_locker;
169
170 Symbol_table* symtab_;
171 Layout* layout_;
172 Input_objects* input_objects_;
173 Archive* archive_;
174 Input_group* input_group_;
175 Task_token* this_blocker_;
176 Task_token* next_blocker_;
177 };
178
179 } // End namespace gold.
180
181 #endif // !defined(GOLD_ARCHIVE_H)