Bug 1747783 - Use unsigned chars for fields that can have values > 127. r=gsvelto

Differential Revision: https://phabricator.services.mozilla.com/D134763
This commit is contained in:
Mike Hommey 2021-12-30 20:52:55 +00:00
Родитель f0cc3222ef
Коммит 837162a71b
3 изменённых файлов: 67 добавлений и 50 удалений

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

@ -99,7 +99,8 @@ static const Elf64_Shdr null64_section = {0, SHT_NULL, 0, 0, 0,
Elf_Shdr null_section(null64_section);
Elf_Ehdr::Elf_Ehdr(std::ifstream& file, char ei_class, char ei_data)
Elf_Ehdr::Elf_Ehdr(std::ifstream& file, unsigned char ei_class,
unsigned char ei_data)
: serializable<Elf_Ehdr_Traits>(file, ei_class, ei_data),
ElfSection(null_section, nullptr, nullptr) {
shdr.sh_size = Elf_Ehdr::size(ei_class);
@ -111,9 +112,9 @@ Elf::Elf(std::ifstream& file) {
file.exceptions(std::ifstream::eofbit | std::ifstream::failbit |
std::ifstream::badbit);
// Read ELF magic number and identification information
char e_ident[EI_VERSION];
unsigned char e_ident[EI_VERSION];
file.seekg(0);
file.read(e_ident, sizeof(e_ident));
file.read((char*)e_ident, sizeof(e_ident));
file.seekg(0);
ehdr = new Elf_Ehdr(file, e_ident[EI_CLASS], e_ident[EI_DATA]);
@ -810,8 +811,8 @@ ElfDynamic_Section::~ElfDynamic_Section() {
delete dyns[i].value;
}
void ElfDynamic_Section::serialize(std::ofstream& file, char ei_class,
char ei_data) {
void ElfDynamic_Section::serialize(std::ofstream& file, unsigned char ei_class,
unsigned char ei_data) {
for (unsigned int i = 0; i < shdr.sh_size / shdr.sh_entsize; i++) {
Elf_Dyn dyn;
dyn.d_tag = dyns[i].tag;
@ -842,8 +843,8 @@ ElfSymtab_Section::ElfSymtab_Section(Elf_Shdr& s, std::ifstream* file,
file->seekg(pos);
}
void ElfSymtab_Section::serialize(std::ofstream& file, char ei_class,
char ei_data) {
void ElfSymtab_Section::serialize(std::ofstream& file, unsigned char ei_class,
unsigned char ei_data) {
ElfStrtab_Section* strtab = (ElfStrtab_Section*)getLink();
for (unsigned int i = 0; i < shdr.sh_size / shdr.sh_entsize; i++) {
Elf_Sym sym;
@ -925,8 +926,8 @@ unsigned int ElfStrtab_Section::getStrIndex(const char* string) {
return 0;
}
void ElfStrtab_Section::serialize(std::ofstream& file, char ei_class,
char ei_data) {
void ElfStrtab_Section::serialize(std::ofstream& file, unsigned char ei_class,
unsigned char ei_data) {
file.seekp(getOffset());
for (std::vector<table_storage>::iterator t = table.begin(); t != table.end();
t++)

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

@ -67,7 +67,8 @@ class ElfRelHack_Section : public ElfSection {
name = elfhack_data;
};
void serialize(std::ofstream& file, char ei_class, char ei_data) {
void serialize(std::ofstream& file, unsigned char ei_class,
unsigned char ei_data) {
for (std::vector<Elf_RelHack>::iterator i = rels.begin(); i != rels.end();
++i)
(*i).serialize(file, ei_class, ei_data);
@ -194,7 +195,8 @@ class ElfRelHackCode_Section : public ElfSection {
~ElfRelHackCode_Section() { delete elf; }
void serialize(std::ofstream& file, char ei_class, char ei_data) override {
void serialize(std::ofstream& file, unsigned char ei_class,
unsigned char ei_data) override {
// Readjust code offsets
for (std::vector<ElfSection*>::iterator c = code.begin(); c != code.end();
++c)
@ -519,23 +521,25 @@ void maybe_split_segment(Elf* elf, ElfSegment* segment) {
}
// EH_FRAME constants
static const char DW_EH_PE_absptr = 0x00;
static const char DW_EH_PE_omit = 0xff;
static const unsigned char DW_EH_PE_absptr = 0x00;
static const unsigned char DW_EH_PE_omit = 0xff;
// Data size
static const char DW_EH_PE_LEB128 = 0x01;
static const char DW_EH_PE_data2 = 0x02;
static const char DW_EH_PE_data4 = 0x03;
static const char DW_EH_PE_data8 = 0x04;
static const unsigned char DW_EH_PE_LEB128 = 0x01;
static const unsigned char DW_EH_PE_data2 = 0x02;
static const unsigned char DW_EH_PE_data4 = 0x03;
static const unsigned char DW_EH_PE_data8 = 0x04;
// Data signedness
static const char DW_EH_PE_signed = 0x08;
static const unsigned char DW_EH_PE_signed = 0x08;
// Modifiers
static const char DW_EH_PE_pcrel = 0x10;
static const unsigned char DW_EH_PE_pcrel = 0x10;
// Return the data size part of the encoding value
static char encoding_data_size(char encoding) { return encoding & 0x07; }
static unsigned char encoding_data_size(unsigned char encoding) {
return encoding & 0x07;
}
// Advance `step` bytes in the buffer at `data` with size `size`, returning
// the advanced buffer pointer and remaining size.
@ -561,7 +565,8 @@ static bool skip_LEB128(char** data, size_t* size) {
// Advance in the given buffer, skipping the full length of a pointer encoded
// with the given encoding.
static bool skip_eh_frame_pointer(char** data, size_t* size, char encoding) {
static bool skip_eh_frame_pointer(char** data, size_t* size,
unsigned char encoding) {
switch (encoding_data_size(encoding)) {
case DW_EH_PE_data2:
return advance_buffer(data, size, 2);
@ -602,7 +607,8 @@ static bool adjust_eh_frame_sized_pointer(char** data, size_t* size,
// In the given eh_frame section, adjust the pointer with the given encoding,
// pointed to by the given buffer (`data`, `size`), considering the eh_frame
// section was originally at `origAddr`. Also advances in the buffer.
static bool adjust_eh_frame_pointer(char** data, size_t* size, char encoding,
static bool adjust_eh_frame_pointer(char** data, size_t* size,
unsigned char encoding,
ElfSection* eh_frame, unsigned int origAddr,
Elf* elf) {
if ((encoding & 0x70) != DW_EH_PE_pcrel)
@ -647,8 +653,8 @@ static void adjust_eh_frame(ElfSection* eh_frame, unsigned int origAddr,
char* data = const_cast<char*>(eh_frame->getData());
size_t size = eh_frame->getSize();
char LSDAencoding = DW_EH_PE_omit;
char FDEencoding = DW_EH_PE_absptr;
unsigned char LSDAencoding = DW_EH_PE_omit;
unsigned char FDEencoding = DW_EH_PE_absptr;
bool hasZ = false;
// Decoding of eh_frame based on https://www.airs.com/blog/archives/460
@ -718,7 +724,7 @@ static void adjust_eh_frame(ElfSection* eh_frame, unsigned int origAddr,
length--;
break;
case 'P': {
char encoding = *cursor++;
unsigned char encoding = (unsigned char)*cursor++;
length--;
if (!adjust_eh_frame_pointer(&cursor, &length, encoding, eh_frame,
origAddr, elf))

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

@ -182,7 +182,7 @@ class serializable : public T::Type64 {
private:
template <typename R>
void init(const char* buf, size_t len, char ei_data) {
void init(const char* buf, size_t len, unsigned char ei_data) {
R e;
assert(len >= sizeof(e));
memcpy(&e, buf, sizeof(e));
@ -197,7 +197,7 @@ class serializable : public T::Type64 {
}
template <typename R>
void serialize(const char* buf, size_t len, char ei_data) {
void serialize(const char* buf, size_t len, unsigned char ei_data) {
assert(len >= sizeof(R));
if (ei_data == ELFDATA2LSB) {
T::template swap<little_endian>(*this, *(R*)buf);
@ -210,7 +210,8 @@ class serializable : public T::Type64 {
}
public:
serializable(const char* buf, size_t len, char ei_class, char ei_data) {
serializable(const char* buf, size_t len, unsigned char ei_class,
unsigned char ei_data) {
if (ei_class == ELFCLASS32) {
init<typename T::Type32>(buf, len, ei_data);
return;
@ -221,7 +222,8 @@ class serializable : public T::Type64 {
throw std::runtime_error("Unsupported ELF class");
}
serializable(std::ifstream& file, char ei_class, char ei_data) {
serializable(std::ifstream& file, unsigned char ei_class,
unsigned char ei_data) {
if (ei_class == ELFCLASS32) {
typename T::Type32 e;
file.read((char*)&e, sizeof(e));
@ -236,7 +238,8 @@ class serializable : public T::Type64 {
throw std::runtime_error("Unsupported ELF class or data encoding");
}
void serialize(std::ofstream& file, char ei_class, char ei_data) {
void serialize(std::ofstream& file, unsigned char ei_class,
unsigned char ei_data) {
if (ei_class == ELFCLASS32) {
typename T::Type32 e;
serialize<typename T::Type32>((char*)&e, sizeof(e), ei_data);
@ -251,7 +254,8 @@ class serializable : public T::Type64 {
throw std::runtime_error("Unsupported ELF class or data encoding");
}
void serialize(char* buf, size_t len, char ei_class, char ei_data) {
void serialize(char* buf, size_t len, unsigned char ei_class,
unsigned char ei_data) {
if (ei_class == ELFCLASS32) {
serialize<typename T::Type32>(buf, len, ei_data);
return;
@ -262,7 +266,7 @@ class serializable : public T::Type64 {
throw std::runtime_error("Unsupported ELF class");
}
static inline unsigned int size(char ei_class) {
static inline unsigned int size(unsigned char ei_class) {
if (ei_class == ELFCLASS32)
return sizeof(typename T::Type32);
else if (ei_class == ELFCLASS64)
@ -290,10 +294,10 @@ class Elf {
void normalize();
void write(std::ofstream& file);
char getClass();
char getData();
char getType();
char getMachine();
unsigned char getClass();
unsigned char getData();
unsigned char getType();
unsigned char getMachine();
unsigned int getSize();
void insertSegmentAfter(ElfSegment* previous, ElfSegment* segment) {
@ -407,7 +411,8 @@ class ElfSection {
if (next) next->markDirty();
}
virtual void serialize(std::ofstream& file, char ei_class, char ei_data) {
virtual void serialize(std::ofstream& file, unsigned char ei_class,
unsigned char ei_data) {
if (getType() == SHT_NOBITS) return;
file.seekp(getOffset());
file.write(data, getSize());
@ -480,8 +485,9 @@ class ElfSegment {
class Elf_Ehdr : public serializable<Elf_Ehdr_Traits>, public ElfSection {
public:
Elf_Ehdr(std::ifstream& file, char ei_class, char ei_data);
void serialize(std::ofstream& file, char ei_class, char ei_data) {
Elf_Ehdr(std::ifstream& file, unsigned char ei_class, unsigned char ei_data);
void serialize(std::ofstream& file, unsigned char ei_class,
unsigned char ei_data) {
serializable<Elf_Ehdr_Traits>::serialize(file, ei_class, ei_data);
}
};
@ -489,7 +495,7 @@ class Elf_Ehdr : public serializable<Elf_Ehdr_Traits>, public ElfSection {
class Elf_Phdr : public serializable<Elf_Phdr_Traits> {
public:
Elf_Phdr(){};
Elf_Phdr(std::ifstream& file, char ei_class, char ei_data)
Elf_Phdr(std::ifstream& file, unsigned char ei_class, unsigned char ei_data)
: serializable<Elf_Phdr_Traits>(file, ei_class, ei_data){};
bool contains(ElfSection* section) {
unsigned int size = section->getSize();
@ -518,7 +524,8 @@ class ElfDynamic_Section : public ElfSection {
ElfDynamic_Section(Elf_Shdr& s, std::ifstream* file, Elf* parent);
~ElfDynamic_Section();
void serialize(std::ofstream& file, char ei_class, char ei_data);
void serialize(std::ofstream& file, unsigned char ei_class,
unsigned char ei_data);
ElfValue* getValueForType(unsigned int tag);
ElfSection* getSectionForType(unsigned int tag);
@ -545,7 +552,8 @@ class ElfSymtab_Section : public ElfSection {
public:
ElfSymtab_Section(Elf_Shdr& s, std::ifstream* file, Elf* parent);
void serialize(std::ofstream& file, char ei_class, char ei_data);
void serialize(std::ofstream& file, unsigned char ei_class,
unsigned char ei_data);
Elf_SymValue* lookup(const char* name,
unsigned int type_filter = STT(OBJECT) | STT(FUNC));
@ -558,7 +566,7 @@ class Elf_Rel : public serializable<Elf_Rel_Traits> {
public:
Elf_Rel() : serializable<Elf_Rel_Traits>(){};
Elf_Rel(std::ifstream& file, char ei_class, char ei_data)
Elf_Rel(std::ifstream& file, unsigned char ei_class, unsigned char ei_data)
: serializable<Elf_Rel_Traits>(file, ei_class, ei_data){};
static const unsigned int sh_type = SHT_REL;
@ -570,7 +578,7 @@ class Elf_Rela : public serializable<Elf_Rela_Traits> {
public:
Elf_Rela() : serializable<Elf_Rela_Traits>(){};
Elf_Rela(std::ifstream& file, char ei_class, char ei_data)
Elf_Rela(std::ifstream& file, unsigned char ei_class, unsigned char ei_data)
: serializable<Elf_Rela_Traits>(file, ei_class, ei_data){};
static const unsigned int sh_type = SHT_RELA;
@ -592,7 +600,8 @@ class ElfRel_Section : public ElfSection {
file->seekg(pos);
}
void serialize(std::ofstream& file, char ei_class, char ei_data) {
void serialize(std::ofstream& file, unsigned char ei_class,
unsigned char ei_data) {
for (typename std::vector<Rel>::iterator i = rels.begin(); i != rels.end();
++i)
(*i).serialize(file, ei_class, ei_data);
@ -620,7 +629,8 @@ class ElfStrtab_Section : public ElfSection {
unsigned int getStrIndex(const char* string);
void serialize(std::ofstream& file, char ei_class, char ei_data);
void serialize(std::ofstream& file, unsigned char ei_class,
unsigned char ei_data);
private:
struct table_storage {
@ -634,13 +644,13 @@ class ElfStrtab_Section : public ElfSection {
std::vector<table_storage> table;
};
inline char Elf::getClass() { return ehdr->e_ident[EI_CLASS]; }
inline unsigned char Elf::getClass() { return ehdr->e_ident[EI_CLASS]; }
inline char Elf::getData() { return ehdr->e_ident[EI_DATA]; }
inline unsigned char Elf::getData() { return ehdr->e_ident[EI_DATA]; }
inline char Elf::getType() { return ehdr->e_type; }
inline unsigned char Elf::getType() { return ehdr->e_type; }
inline char Elf::getMachine() { return ehdr->e_machine; }
inline unsigned char Elf::getMachine() { return ehdr->e_machine; }
inline unsigned int Elf::getSize() {
ElfSection* section;