1 // archive.cc -- archive support for gold
19 // The header of an entry in the archive. This is all readable text,
20 // padded with spaces where necesary. If the contents of an archive
21 // are all text file, the entire archive is readable.
23 struct Archive::Archive_header
27 // The file modification time.
29 // The user's UID in decimal.
31 // The user's GID in decimal.
33 // The file mode in octal.
35 // The file size in decimal.
37 // The final magic code.
43 const char Archive::armag
[sarmag
] =
45 '!', '<', 'a', 'r', 'c', 'h', '>', '\n'
48 const char Archive::arfmag
[2] = { '`', '\n' };
50 // Get a view into the underlying file.
53 Archive::get_view(off_t start
, off_t size
)
55 return this->input_file_
->file().get_view(start
, size
);
58 // Set up the archive: read the symbol map and the extended name
64 // The first member of the archive should be the symbol table.
65 std::string armap_name
;
66 off_t armap_size
= this->read_header(sarmag
, &armap_name
);
67 if (!armap_name
.empty())
69 fprintf(stderr
, _("%s: %s: no archive symbol table (run ranlib)\n"),
70 program_name
, this->name().c_str());
74 // Read in the entire armap.
75 const unsigned char* p
= this->get_view(sarmag
+ sizeof(Archive_header
),
78 // Numbers in the armap are always big-endian.
79 const elfcpp::Elf_Word
* pword
= reinterpret_cast<const elfcpp::Elf_Word
*>(p
);
80 unsigned int nsyms
= elfcpp::read_elf_word
<true>(pword
);
83 // Note that the addition is in units of sizeof(elfcpp::Elf_Word).
84 const char* pnames
= reinterpret_cast<const char*>(pword
+ nsyms
);
86 this->armap_
.resize(nsyms
);
88 for (unsigned int i
= 0; i
< nsyms
; ++i
)
90 this->armap_
[i
].name
= pnames
;
91 this->armap_
[i
].offset
= elfcpp::read_elf_word
<true>(pword
);
92 pnames
+= strlen(pnames
) + 1;
96 if (reinterpret_cast<const unsigned char*>(pnames
) - p
> armap_size
)
98 fprintf(stderr
, _("%s: %s: bad archive symbol table names\n"),
99 program_name
, this->name().c_str());
103 // See if there is an extended name table.
104 off_t off
= sarmag
+ sizeof(Archive_header
) + armap_size
;
108 off_t extended_size
= this->read_header(off
, &xname
);
111 p
= this->get_view(off
+ sizeof(Archive_header
), extended_size
);
112 const char* px
= reinterpret_cast<const char*>(p
);
113 this->extended_names_
.assign(px
, extended_size
);
116 // Opening the file locked it. Unlock it now.
117 this->input_file_
->file().unlock();
120 // Read the header of an archive member at OFF. Fail if something
121 // goes wrong. Return the size of the member. Set *PNAME to the name
125 Archive::read_header(off_t off
, std::string
* pname
)
127 const unsigned char* p
= this->get_view(off
, sizeof(Archive_header
));
128 const Archive_header
* hdr
= reinterpret_cast<const Archive_header
*>(p
);
130 if (memcmp(hdr
->ar_fmag
, arfmag
, sizeof arfmag
) != 0)
132 fprintf(stderr
, _("%s; %s: malformed archive header at %ld\n"),
133 program_name
, this->name().c_str(),
134 static_cast<long>(off
));
138 const int size_string_size
= sizeof hdr
->ar_size
;
139 char size_string
[size_string_size
+ 1];
140 memcpy(size_string
, hdr
->ar_size
, size_string_size
);
141 char* ps
= size_string
+ size_string_size
;
142 while (ps
[-1] == ' ')
148 off_t member_size
= strtol(size_string
, &end
, 10);
151 || (member_size
== LONG_MAX
&& errno
== ERANGE
))
153 fprintf(stderr
, _("%s: %s: malformed archive header size at %ld\n"),
154 program_name
, this->name().c_str(),
155 static_cast<long>(off
));
159 if (hdr
->ar_name
[0] != '/')
161 const char* name_end
= strchr(hdr
->ar_name
, '/');
163 || name_end
- hdr
->ar_name
>= static_cast<int>(sizeof hdr
->ar_name
))
165 fprintf(stderr
, _("%s: %s: malformed archive header name at %ld\n"),
166 program_name
, this->name().c_str(),
167 static_cast<long>(off
));
170 pname
->assign(hdr
->ar_name
, name_end
- hdr
->ar_name
);
172 else if (hdr
->ar_name
[1] == ' ')
174 // This is the symbol table.
177 else if (hdr
->ar_name
[1] == '/')
179 // This is the extended name table.
180 pname
->assign(1, '/');
185 long x
= strtol(hdr
->ar_name
+ 1, &end
, 10);
188 || (x
== LONG_MAX
&& errno
== ERANGE
)
189 || static_cast<size_t>(x
) >= this->extended_names_
.size())
191 fprintf(stderr
, _("%s: %s: bad extended name index at %ld\n"),
192 program_name
, this->name().c_str(),
193 static_cast<long>(off
));
197 const char* name
= this->extended_names_
.data() + x
;
198 const char* name_end
= strchr(name
, '/');
199 if (static_cast<size_t>(name_end
- name
) > this->extended_names_
.size()
200 || name_end
[1] != '\n')
202 fprintf(stderr
, _("%s: %s: bad extended name entry at header %ld\n"),
203 program_name
, this->name().c_str(),
204 static_cast<long>(off
));
207 pname
->assign(name
, name_end
- name
);
213 // Select members from the archive and add them to the link. We walk
214 // through the elements in the archive map, and look each one up in
215 // the symbol table. If it exists as a strong undefined symbol, we
216 // pull in the corresponding element. We have to do this in a loop,
217 // since pulling in one element may create new undefined symbols which
218 // may be satisfied by other objects in the archive.
221 Archive::add_symbols(Symbol_table
* symtab
, Input_objects
* input_objects
)
223 size_t armap_size
= this->armap_
.size();
224 std::vector
<bool> seen
;
225 seen
.resize(this->armap_
.size());
228 bool added_new_object
;
231 added_new_object
= false;
233 for (size_t i
= 0; i
< armap_size
; ++i
)
237 if (this->armap_
[i
].offset
== last
)
243 Symbol
* sym
= symtab
->lookup(this->armap_
[i
].name
);
246 else if (sym
->shnum() != elfcpp::SHN_UNDEF
)
251 else if (sym
->binding() == elfcpp::STB_WEAK
)
254 // We want to include this object in the link.
255 last
= this->armap_
[i
].offset
;
256 this->include_member(symtab
, input_objects
, last
);
257 added_new_object
= true;
260 while (added_new_object
);
263 // Include an archive member in the link. OFF is the file offset of
264 // the member header.
267 Archive::include_member(Symbol_table
* symtab
, Input_objects
* input_objects
,
271 this->read_header(off
, &n
);
273 size_t memoff
= off
+ sizeof(Archive_header
);
275 // Read enough of the file to pick up the entire ELF header.
276 int ehdr_size
= elfcpp::Elf_sizes
<64>::ehdr_size
;
278 const unsigned char* p
= this->input_file_
->file().get_view(memoff
,
283 fprintf(stderr
, _("%s: %s: member at %ld is not an ELF object"),
284 program_name
, this->name().c_str(),
285 static_cast<long>(off
));
289 static unsigned char elfmagic
[4] =
291 elfcpp::ELFMAG0
, elfcpp::ELFMAG1
,
292 elfcpp::ELFMAG2
, elfcpp::ELFMAG3
294 if (memcmp(p
, elfmagic
, 4) != 0)
296 fprintf(stderr
, _("%s: %s: member at %ld is not an ELF object"),
297 program_name
, this->name().c_str(),
298 static_cast<long>(off
));
302 Object
* obj
= make_elf_object((std::string(this->input_file_
->name())
304 this->input_file_
, memoff
, p
, bytes
);
306 input_objects
->add_object(obj
);
308 Read_symbols_data sd
= obj
->read_symbols();
309 obj
->add_symbols(symtab
, sd
);
312 // Add_archive_symbols methods.
314 Add_archive_symbols::~Add_archive_symbols()
316 if (this->this_blocker_
!= NULL
)
317 delete this->this_blocker_
;
318 // next_blocker_ is deleted by the task associated with the next
322 // Return whether we can add the archive symbols. We are blocked by
323 // this_blocker_. We block next_blocker_. We also lock the file.
325 Task::Is_runnable_type
326 Add_archive_symbols::is_runnable(Workqueue
*)
328 if (this->this_blocker_
!= NULL
&& this->this_blocker_
->is_blocked())
333 class Add_archive_symbols::Add_archive_symbols_locker
: public Task_locker
336 Add_archive_symbols_locker(Task_token
& token
, Workqueue
* workqueue
,
338 : blocker_(token
, workqueue
), archlock_(*archive
)
342 Task_locker_block blocker_
;
343 Task_locker_obj
<Archive
> archlock_
;
347 Add_archive_symbols::locks(Workqueue
* workqueue
)
349 return new Add_archive_symbols_locker(*this->next_blocker_
,
355 Add_archive_symbols::run(Workqueue
*)
357 this->archive_
->add_symbols(this->symtab_
, this->input_objects_
);
360 } // End namespace gold.