1
1
mirror of https://github.com/rui314/mold.git synced 2024-11-11 16:58:12 +03:00

temporary

This commit is contained in:
Rui Ueyama 2020-12-10 20:54:08 +09:00
parent 6bc9135bfd
commit 619f12a975
3 changed files with 47 additions and 2 deletions

43
elf.cc
View File

@ -35,3 +35,46 @@ std::string_view ElfFile::get_string(u32 idx) const {
error(mb.name + ": invalid section index"); error(mb.name + ": invalid section index");
return get_string(sections[idx]); return get_string(sections[idx]);
} }
struct ArHdr {
std::string get_name() const {
std::string_view view(ar_name);
return std::string(view.substr(0, view.find(' ')));
}
u32 get_size() const {
u32 sz = 0;
for (char c : ar_size)
if (isdigit(c))
sz = sz * 10 + c - '0';
return sz;
}
char ar_name[16];
char ar_date[12];
char ar_uid[6];
char ar_gid[6];
char ar_mode[8];
char ar_size[10];
char ar_fmag[2];
};
std::vector<MemoryMappedFile> read_archive_members(MemoryMappedFile mb) {
if (mb.size < 8 || memcmp(mb.data, "!<arch>\n", 8))
error(mb.name + ": not an archive file");
u8 *data = mb.data + 8;
std::vector<MemoryMappedFile> vec;
while (data < mb.data + mb.size) {
ArHdr &hdr = *(ArHdr *)data;
data += sizeof(ArHdr);
std::string name = hdr.get_name();
u32 size = hdr.get_size();
if (name != "/" && name != "//" && name != "__.SYMDEF")
vec.push_back({name, data, size});
data += size;
}
return vec;
}

View File

@ -146,8 +146,8 @@ MemoryMappedFile must_open_input_file(std::string path) {
void read_file(MemoryMappedFile mb) { void read_file(MemoryMappedFile mb) {
switch (identify_magic({(char *)mb.data, mb.size})) { switch (identify_magic({(char *)mb.data, mb.size})) {
case file_magic::archive: case file_magic::archive:
for (MemoryMappedFile member : get_archive_members(mb)) for (MemoryMappedFile &child : read_archive_members(mb))
out::objs.push_back(new ObjectFile(member, mb.name)); out::objs.push_back(new ObjectFile(child, mb.name));
break; break;
case file_magic::elf_relocatable: case file_magic::elf_relocatable:
out::objs.push_back(new ObjectFile(mb, "")); out::objs.push_back(new ObjectFile(mb, ""));

2
mold.h
View File

@ -274,6 +274,8 @@ private:
std::span<ElfShdr> sections; std::span<ElfShdr> sections;
}; };
std::vector<MemoryMappedFile> read_archive_members(MemoryMappedFile mb);
// //
// Symbol // Symbol
// //