2021-10-05 05:24:44 +03:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "mold.h"
|
2022-01-02 13:29:02 +03:00
|
|
|
#include "filetype.h"
|
2021-10-05 05:24:44 +03:00
|
|
|
|
|
|
|
namespace mold {
|
|
|
|
|
|
|
|
struct ArHdr {
|
|
|
|
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];
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename C>
|
|
|
|
std::vector<MappedFile<C> *>
|
|
|
|
read_thin_archive_members(C &ctx, MappedFile<C> *mf) {
|
|
|
|
u8 *begin = mf->data;
|
|
|
|
u8 *data = begin + 8;
|
|
|
|
std::vector<MappedFile<C> *> vec;
|
|
|
|
std::string_view strtab;
|
|
|
|
|
|
|
|
while (data < begin + mf->size) {
|
|
|
|
// Each header is aligned to a 2 byte boundary.
|
|
|
|
if ((begin - data) % 2)
|
|
|
|
data++;
|
|
|
|
|
|
|
|
ArHdr &hdr = *(ArHdr *)data;
|
|
|
|
u8 *body = data + sizeof(hdr);
|
|
|
|
u64 size = atol(hdr.ar_size);
|
|
|
|
|
|
|
|
// Read a string table.
|
2021-12-22 06:08:58 +03:00
|
|
|
if (memcmp(hdr.ar_name, "// ", 3) == 0) {
|
2022-01-02 16:47:17 +03:00
|
|
|
strtab = {(char *)body, (size_t)size};
|
2021-10-05 05:24:44 +03:00
|
|
|
data = body + size;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip a symbol table.
|
2021-12-22 06:08:58 +03:00
|
|
|
if (memcmp(hdr.ar_name, "/ ", 2) == 0) {
|
2021-10-05 05:24:44 +03:00
|
|
|
data = body + size;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hdr.ar_name[0] != '/')
|
|
|
|
Fatal(ctx) << mf->name << ": filename is not stored as a long filename";
|
|
|
|
|
|
|
|
const char *start = strtab.data() + atoi(hdr.ar_name + 1);
|
|
|
|
std::string name(start, (const char *)strstr(start, "/\n"));
|
|
|
|
std::string path = name.starts_with('/') ?
|
2021-12-31 10:21:52 +03:00
|
|
|
name : (filepath(mf->name).parent_path() / name).string();
|
2021-10-05 05:24:44 +03:00
|
|
|
vec.push_back(MappedFile<C>::must_open(ctx, path));
|
|
|
|
data = body;
|
|
|
|
}
|
|
|
|
return vec;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename C>
|
|
|
|
std::vector<MappedFile<C> *>
|
|
|
|
read_fat_archive_members(C &ctx, MappedFile<C> *mf) {
|
|
|
|
u8 *begin = mf->data;
|
|
|
|
u8 *data = begin + 8;
|
|
|
|
std::vector<MappedFile<C> *> vec;
|
|
|
|
std::string_view strtab;
|
|
|
|
|
|
|
|
while (begin + mf->size - data >= 2) {
|
|
|
|
if ((begin - data) % 2)
|
|
|
|
data++;
|
|
|
|
|
|
|
|
ArHdr &hdr = *(ArHdr *)data;
|
|
|
|
u8 *body = data + sizeof(hdr);
|
|
|
|
u64 size = atol(hdr.ar_size);
|
|
|
|
data = body + size;
|
|
|
|
|
2021-10-27 16:16:07 +03:00
|
|
|
// Read if string table
|
2021-12-22 06:08:58 +03:00
|
|
|
if (memcmp(hdr.ar_name, "// ", 3) == 0) {
|
2022-01-02 16:47:17 +03:00
|
|
|
strtab = {(char *)body, (size_t)size};
|
2021-10-05 05:24:44 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-10-27 16:16:07 +03:00
|
|
|
// Skip if symbol table
|
2022-01-13 17:33:20 +03:00
|
|
|
if (memcmp(hdr.ar_name, "/ ", 2) == 0 ||
|
|
|
|
memcmp(hdr.ar_name, "/SYM64/ ", 8) == 0)
|
2021-10-05 05:24:44 +03:00
|
|
|
continue;
|
|
|
|
|
2021-10-27 16:16:07 +03:00
|
|
|
// Read the name field
|
2021-10-05 05:24:44 +03:00
|
|
|
std::string name;
|
|
|
|
|
2021-12-22 06:08:58 +03:00
|
|
|
if (memcmp(hdr.ar_name, "#1/", 3) == 0) {
|
2021-10-27 16:16:07 +03:00
|
|
|
size_t namelen = (size_t)atoi(hdr.ar_name + 3);
|
|
|
|
name = {(char *)(&hdr + 1), namelen};
|
|
|
|
if (size_t pos = name.find('\0'))
|
|
|
|
name = name.substr(0, pos);
|
|
|
|
body += namelen;
|
|
|
|
} else if (hdr.ar_name[0] == '/') {
|
2021-10-05 05:24:44 +03:00
|
|
|
const char *start = strtab.data() + atoi(hdr.ar_name + 1);
|
|
|
|
name = {start, (const char *)strstr(start, "/\n")};
|
|
|
|
} else {
|
2021-10-27 16:16:07 +03:00
|
|
|
char *end = (char *)memchr(hdr.ar_name, '/', sizeof(hdr.ar_name));
|
|
|
|
if (!end)
|
|
|
|
end = hdr.ar_name + sizeof(hdr.ar_name);
|
|
|
|
name = {hdr.ar_name, end};
|
2021-10-05 05:24:44 +03:00
|
|
|
}
|
|
|
|
|
2021-10-27 16:16:07 +03:00
|
|
|
// Skip if symbol table
|
|
|
|
if (name == "__.SYMDEF" || name == "__.SYMDEF SORTED")
|
|
|
|
continue;
|
|
|
|
|
2021-10-05 05:24:44 +03:00
|
|
|
vec.push_back(mf->slice(ctx, name, body - begin, size));
|
|
|
|
}
|
|
|
|
return vec;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename C>
|
|
|
|
std::vector<MappedFile<C> *>
|
|
|
|
read_archive_members(C &ctx, MappedFile<C> *mf) {
|
|
|
|
switch (get_file_type(mf)) {
|
|
|
|
case FileType::AR:
|
|
|
|
return read_fat_archive_members(ctx, mf);
|
|
|
|
case FileType::THIN_AR:
|
|
|
|
return read_thin_archive_members(ctx, mf);
|
|
|
|
default:
|
|
|
|
unreachable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mold
|