Bug 1495733 - Fix elfhack for files > 2GiB and < 4GiB. r=gsvelto

This only solves the easy half of the problem outlined in the bug,
leaving the other half for later.

iostream::tellg() actually returns streampos, which is able to support
files larger than 4GiB with libstdc++, but converting to an int
obviously truncated that, as well as transformed values between 2GiB and
4GiB into invalid negative numbers.

iostream::seekg() also takes a streampos, so storing the streampos as-is
is enough to address the problem with tellg()/seekg() sequences.

The other half of the problem involves elfhack converting 64-bits ELF
headers to 32-bits headers internally, which requires deeper changes.

This change however, is enough to support files up to 4GiB, which is
already a good first step.

Differential Revision: https://phabricator.services.mozilla.com/D94252
This commit is contained in:
Mike Hommey 2020-10-21 07:30:38 +00:00
Родитель 4e6e6c7714
Коммит 5c05913d21
2 изменённых файлов: 4 добавлений и 4 удалений

Просмотреть файл

@ -509,7 +509,7 @@ ElfSection::ElfSection(Elf_Shdr& s, std::ifstream* file, Elf* parent)
if (!data) {
throw std::runtime_error("Could not malloc ElfSection data");
}
int pos = file->tellg();
auto pos = file->tellg();
file->seekg(shdr.sh_offset);
file->read(data, shdr.sh_size);
file->seekg(pos);
@ -722,7 +722,7 @@ bool ElfDynamic_Section::setValueForType(unsigned int tag, ElfValue* val) {
ElfDynamic_Section::ElfDynamic_Section(Elf_Shdr& s, std::ifstream* file,
Elf* parent)
: ElfSection(s, file, parent) {
int pos = file->tellg();
auto pos = file->tellg();
dyns.resize(s.sh_size / s.sh_entsize);
file->seekg(shdr.sh_offset);
// Here we assume tags refer to only one section (e.g. DT_RELSZ accounts
@ -823,7 +823,7 @@ void ElfDynamic_Section::serialize(std::ofstream& file, char ei_class,
ElfSymtab_Section::ElfSymtab_Section(Elf_Shdr& s, std::ifstream* file,
Elf* parent)
: ElfSection(s, file, parent) {
int pos = file->tellg();
auto pos = file->tellg();
syms.resize(s.sh_size / s.sh_entsize);
ElfStrtab_Section* strtab = (ElfStrtab_Section*)getLink();
file->seekg(shdr.sh_offset);

Просмотреть файл

@ -583,7 +583,7 @@ class ElfRel_Section : public ElfSection {
public:
ElfRel_Section(Elf_Shdr& s, std::ifstream* file, Elf* parent)
: ElfSection(s, file, parent) {
int pos = file->tellg();
auto pos = file->tellg();
file->seekg(shdr.sh_offset);
for (unsigned int i = 0; i < s.sh_size / s.sh_entsize; i++) {
Rel r(*file, parent->getClass(), parent->getData());