mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 17:57:34 +00:00
AK: Make MappedFile heap-allocated and ref-counted
Let's adapt this class a bit better to how it's actually being used. Instead of having valid/invalid states and storing an error in case it's invalid, a MappedFile is now always valid, and the factory function that creates it will return an OSError if mapping fails.
This commit is contained in:
parent
70fce5c4c7
commit
2f3b901f7f
36 changed files with 184 additions and 199 deletions
|
@ -48,12 +48,14 @@ int main(int argc, char** argv)
|
|||
args_parser.add_positional_argument(path, "Path to i386 binary file", "path");
|
||||
args_parser.parse(argc, argv);
|
||||
|
||||
MappedFile file(path);
|
||||
if (!file.is_valid()) {
|
||||
// Already printed some error message.
|
||||
auto file_or_error = MappedFile::map(path);
|
||||
if (file_or_error.is_error()) {
|
||||
warnln("Could not map file: {}", file_or_error.error().string());
|
||||
return 1;
|
||||
}
|
||||
|
||||
auto& file = *file_or_error.value();
|
||||
|
||||
struct Symbol {
|
||||
size_t value;
|
||||
size_t size;
|
||||
|
|
|
@ -90,7 +90,7 @@ static NonnullOwnPtr<HashMap<void*, X86::Instruction>> instrument_code()
|
|||
if (section.name() != ".text")
|
||||
return IterationDecision::Continue;
|
||||
|
||||
X86::SimpleInstructionStream stream((const u8*)((u32)lib.file.data() + section.offset()), section.size());
|
||||
X86::SimpleInstructionStream stream((const u8*)((u32)lib.file->data() + section.offset()), section.size());
|
||||
X86::Disassembler disassembler(stream);
|
||||
for (;;) {
|
||||
auto offset = stream.offset();
|
||||
|
|
|
@ -284,14 +284,15 @@ int main(int argc, char** argv)
|
|||
display_symbol_table = true;
|
||||
}
|
||||
|
||||
MappedFile mapped_executable(path);
|
||||
auto file_or_error = MappedFile::map(path);
|
||||
|
||||
if (!mapped_executable.is_valid()) {
|
||||
fprintf(stderr, "Unable to map file %s\n", path);
|
||||
if (file_or_error.is_error()) {
|
||||
warnln("Unable to map file {}: {}", path, file_or_error.error());
|
||||
return -1;
|
||||
}
|
||||
|
||||
ELF::Image executable_elf((const u8*)mapped_executable.data(), mapped_executable.size());
|
||||
auto elf_image_data = file_or_error.value()->bytes();
|
||||
ELF::Image executable_elf(elf_image_data);
|
||||
|
||||
if (!executable_elf.is_valid()) {
|
||||
fprintf(stderr, "File is not a valid ELF object\n");
|
||||
|
@ -300,7 +301,7 @@ int main(int argc, char** argv)
|
|||
|
||||
String interpreter_path;
|
||||
|
||||
if (!ELF::validate_program_headers(*(Elf32_Ehdr*)mapped_executable.data(), mapped_executable.size(), (u8*)mapped_executable.data(), mapped_executable.size(), &interpreter_path)) {
|
||||
if (!ELF::validate_program_headers(*(const Elf32_Ehdr*)elf_image_data.data(), elf_image_data.size(), (const u8*)elf_image_data.data(), elf_image_data.size(), &interpreter_path)) {
|
||||
fprintf(stderr, "Invalid ELF headers\n");
|
||||
return -1;
|
||||
}
|
||||
|
@ -309,14 +310,14 @@ int main(int argc, char** argv)
|
|||
fprintf(stderr, "Warning: Dynamic ELF object has no interpreter path\n");
|
||||
}
|
||||
|
||||
ELF::Image interpreter_image((const u8*)mapped_executable.data(), mapped_executable.size());
|
||||
ELF::Image interpreter_image(elf_image_data);
|
||||
|
||||
if (!interpreter_image.is_valid()) {
|
||||
fprintf(stderr, "ELF image is invalid\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
auto header = *reinterpret_cast<const Elf32_Ehdr*>(mapped_executable.data());
|
||||
auto& header = *reinterpret_cast<const Elf32_Ehdr*>(elf_image_data.data());
|
||||
|
||||
if (display_elf_header) {
|
||||
printf("ELF header:\n");
|
||||
|
|
|
@ -181,9 +181,12 @@ int main(int argc, char** argv)
|
|||
return 1;
|
||||
}
|
||||
|
||||
MappedFile mapped_file { zip_file_path };
|
||||
if (!mapped_file.is_valid())
|
||||
auto file_or_error = MappedFile ::map(zip_file_path);
|
||||
if (file_or_error.is_error()) {
|
||||
warnln("Failed to open {}: {}", zip_file_path, file_or_error.error());
|
||||
return 1;
|
||||
}
|
||||
auto& mapped_file = *file_or_error.value();
|
||||
|
||||
printf("Archive: %s\n", zip_file_path.characters());
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue