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-13 20:51:27 +09:00
parent 86045d8f69
commit cab56f97ff

View File

@ -802,17 +802,44 @@ struct ArHdr {
char ar_fmag[2];
};
std::vector<MemoryMappedFile> read_archive_members(MemoryMappedFile mb) {
if (mb.size < 8)
error(mb.name + ": not an archive file");
if (memcmp(mb.data, "!<arch>\n", 8) && memcmp(mb.data, "!<thin>\n", 8))
error(mb.name + ": not an archive file");
bool is_thin = !memcmp(mb.data, "!<thin>\n", 8);
std::vector<MemoryMappedFile> read_thin_archive_members(MemoryMappedFile mb) {
u8 *data = mb.data + 8;
std::vector<MemoryMappedFile> vec;
std::string_view strtab;
std::string basedir = mb.name.substr(0, mb.name.find_last_of('/'));
u8 *data = mb.data + 8;
while (data < mb.data + mb.size) {
ArHdr &hdr = *(ArHdr *)data;
u8 *body = data + sizeof(hdr);
u64 size = atol(hdr.ar_size);
if (!memcmp(hdr.ar_name, "// ", 3)) {
strtab = {(char *)body, size};
data = body + size;
continue;
}
if (!memcmp(hdr.ar_name, "/ ", 2)) {
data = body + size;
continue;
}
if (hdr.ar_name[0] != '/')
error(mb.name + ": filename is not stored as a long filename");
const char *start = strtab.data() + atoi(hdr.ar_name + 1);
std::string name = {start, strstr(start, "/\n")};
MemoryMappedFile mb = must_open_input_file(basedir + "/" + name);
mb.name = name;
vec.push_back(mb);
data = body;
}
return vec;
}
std::vector<MemoryMappedFile> read_fat_archive_members(MemoryMappedFile mb) {
u8 *data = mb.data + 8;
std::vector<MemoryMappedFile> vec;
std::string_view strtab;
@ -839,14 +866,19 @@ std::vector<MemoryMappedFile> read_archive_members(MemoryMappedFile mb) {
name = {hdr.ar_name, strchr(hdr.ar_name, '/')};
}
if (is_thin) {
MemoryMappedFile mb = must_open_input_file(basedir + "/" + name);
mb.name = name;
vec.push_back(mb);
data -= size;
} else
vec.push_back({name, body, size});
vec.push_back({name, body, size});
}
return vec;
}
std::vector<MemoryMappedFile> read_archive_members(MemoryMappedFile mb) {
if (mb.size < 8)
error(mb.name + ": not an archive file");
if (memcmp(mb.data, "!<arch>\n", 8) && memcmp(mb.data, "!<thin>\n", 8))
error(mb.name + ": not an archive file");
bool is_thin = !memcmp(mb.data, "!<thin>\n", 8);
if (is_thin)
return read_thin_archive_members(mb);
return read_fat_archive_members(mb);
}