Fix some warnings/suggestions from clang-tidy.

@DirtyHairy, hope you don't mind.
This commit is contained in:
Stephen Anthony 2024-07-15 13:21:26 -02:30
parent 3f5c331024
commit 1e964828b4
7 changed files with 87 additions and 93 deletions

View File

@ -35,32 +35,32 @@ namespace {
#ifdef DUMP_ELF #ifdef DUMP_ELF
void dumpElf(const ElfFile& elf) void dumpElf(const ElfFile& elf)
{ {
cout << std::endl << "ELF sections:" << std::endl << std::endl; cout << "\nELF sections:\n\n";
size_t i = 0; size_t i = 0;
for (auto& section: elf.getSections()) { for (const auto& section: elf.getSections()) {
if (section.type != 0x00) cout << i << " " << section << std::endl; if (section.type != 0x00) cout << i << " " << section << '\n';
i++; i++;
} }
auto symbols = elf.getSymbols(); auto symbols = elf.getSymbols();
cout << std::endl << "ELF symbols:" << std::endl << std::endl; cout << "\nELF symbols:\n\n";
if (symbols.size() > 0) { if (!symbols.empty()) {
i = 0; i = 0;
for (auto& symbol: symbols) for (auto& symbol: symbols)
cout << (i++) << " " << symbol << std::endl; cout << (i++) << " " << symbol << '\n';
} }
i = 0; i = 0;
for (auto& section: elf.getSections()) { for (const auto& section: elf.getSections()) {
auto rels = elf.getRelocations(i++); auto rels = elf.getRelocations(i++);
if (!rels) continue; if (!rels) continue;
cout cout
<< std::endl << "ELF relocations for section " << "\nELF relocations for section "
<< section.name << ":" << std::endl << std::endl; << section.name << ":\n\n";
for (auto& rel: *rels) cout << rel << std::endl; for (auto& rel: *rels) cout << rel << '\n';
} }
} }
@ -69,15 +69,12 @@ namespace {
cout << std::hex << std::setfill('0'); cout << std::hex << std::setfill('0');
cout cout
<< std::endl << "\ntext segment size: 0x" << std::setw(8) << linker.getSegmentSize(ElfLinker::SegmentType::text)
<< "text segment size: 0x" << std::setw(8) << linker.getSegmentSize(ElfLinker::SegmentType::text) << "\ndata segment size: 0x" << std::setw(8) << linker.getSegmentSize(ElfLinker::SegmentType::data)
<< std::endl << "\nrodata segment size: 0x" << std::setw(8) << linker.getSegmentSize(ElfLinker::SegmentType::rodata)
<< "data segment size: 0x" << std::setw(8) << linker.getSegmentSize(ElfLinker::SegmentType::data) << '\n';
<< std::endl
<< "rodata segment size: 0x" << std::setw(8) << linker.getSegmentSize(ElfLinker::SegmentType::rodata)
<< std::endl;
cout << std::endl << "relocated sections:" << std::endl << std::endl; cout << "\nrelocated sections:\n\n";
const auto& sections = parser.getSections(); const auto& sections = parser.getSections();
const auto& relocatedSections = linker.getRelocatedSections(); const auto& relocatedSections = linker.getRelocatedSections();
@ -89,10 +86,10 @@ namespace {
<< sections[i].name << sections[i].name
<< " @ 0x"<< std::setw(8) << " @ 0x"<< std::setw(8)
<< (relocatedSections[i]->offset + linker.getSegmentBase(relocatedSections[i]->segment)) << (relocatedSections[i]->offset + linker.getSegmentBase(relocatedSections[i]->segment))
<< " size 0x" << std::setw(8) << sections[i].size << std::endl; << " size 0x" << std::setw(8) << sections[i].size << '\n';
} }
cout << std::endl << "relocated symbols:" << std::endl << std::endl; cout << "\nrelocated symbols:\n\n";
const auto& symbols = parser.getSymbols(); const auto& symbols = parser.getSymbols();
const auto& relocatedSymbols = linker.getRelocatedSymbols(); const auto& relocatedSymbols = linker.getRelocatedSymbols();
@ -122,30 +119,30 @@ namespace {
cout << " (abs)"; cout << " (abs)";
} }
cout << std::endl; cout << '\n';
} }
const auto& initArray = linker.getInitArray(); const auto& initArray = linker.getInitArray();
const auto& preinitArray = linker.getPreinitArray(); const auto& preinitArray = linker.getPreinitArray();
if (initArray.size()) { if (!initArray.empty()) {
cout << std::endl << "init array:" << std::endl << std::endl; cout << "\ninit array:\n\n";
for (uInt32 address: initArray) for (const uInt32 address: initArray)
cout << " * 0x" << std::setw(8) << address << std::endl; cout << " * 0x" << std::setw(8) << address << '\n';
} }
if (preinitArray.size()) { if (!preinitArray.empty()) {
cout << std::endl << "preinit array:" << std::endl << std::endl; cout << "\npreinit array:\n\n";
for (uInt32 address: preinitArray) for (const uInt32 address: preinitArray)
cout << " * 0x" << std::setw(8) << address << std::endl; cout << " * 0x" << std::setw(8) << address << '\n';
} }
cout << std::dec; cout << std::dec;
} }
#endif #endif
} } // namespace
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
CartridgeELF::CartridgeELF(const ByteBuffer& image, size_t size, string_view md5, CartridgeELF::CartridgeELF(const ByteBuffer& image, size_t size, string_view md5,
@ -189,16 +186,15 @@ CartridgeELF::CartridgeELF(const ByteBuffer& image, size_t size, string_view md5
dumpLinkage(elfParser, elfLinker); dumpLinkage(elfParser, elfLinker);
cout cout
<< std::endl << "\nARM entrypoint: 0x"
<< "ARM entrypoint: 0x"
<< std::hex << std::setw(8) << std::setfill('0') << myArmEntrypoint << std::dec << std::hex << std::setw(8) << std::setfill('0') << myArmEntrypoint << std::dec
<< std::endl; << '\n';
#endif #endif
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
CartridgeELF::~CartridgeELF() {} CartridgeELF::~CartridgeELF() = default;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void CartridgeELF::reset() void CartridgeELF::reset()
@ -221,7 +217,7 @@ void CartridgeELF::install(System& system)
{ {
mySystem = &system; mySystem = &system;
for (size_t addr = 0; addr < 0x1000; addr += System::PAGE_SIZE) { for (uInt16 addr = 0; addr < 0x1000; addr += System::PAGE_SIZE) {
System::PageAccess access(this, System::PageAccessType::READ); System::PageAccess access(this, System::PageAccessType::READ);
access.romPeekCounter = &myRomAccessCounter[addr]; access.romPeekCounter = &myRomAccessCounter[addr];
access.romPokeCounter = &myRomAccessCounter[addr]; access.romPokeCounter = &myRomAccessCounter[addr];
@ -308,7 +304,7 @@ void CartridgeELF::vcsWrite5(uInt8 zpAddress, uInt8 value)
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void CartridgeELF::vcsCopyOverblankToRiotRam() void CartridgeELF::vcsCopyOverblankToRiotRam()
{ {
for (size_t i = 0; i < OVERBLANK_PROGRAM_SIZE; i++) for (uInt8 i = 0; i < OVERBLANK_PROGRAM_SIZE; i++)
vcsWrite5(0x80 + i, OVERBLANK_PROGRAM[i]); vcsWrite5(0x80 + i, OVERBLANK_PROGRAM[i]);
} }
@ -337,13 +333,13 @@ CartridgeELF::BusTransaction CartridgeELF::BusTransaction::transactionDrive(uInt
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void CartridgeELF::BusTransaction::setBusState(bool& drive, uInt8& value) void CartridgeELF::BusTransaction::setBusState(bool& bs_drive, uInt8& bs_value) const
{ {
if (yield) { if (yield) {
drive = false; bs_drive = false;
} else { } else {
drive = true; bs_drive = true;
value = this->value; bs_value = this->value;
} }
} }

View File

@ -70,7 +70,7 @@ class CartridgeELF: public Cartridge {
static BusTransaction transactionYield(uInt16 address); static BusTransaction transactionYield(uInt16 address);
static BusTransaction transactionDrive(uInt16 address, uInt8 value); static BusTransaction transactionDrive(uInt16 address, uInt8 value);
void setBusState(bool& drive, uInt8& value); void setBusState(bool& drive, uInt8& value) const;
uInt16 address; uInt16 address;
uInt8 value; uInt8 value;

View File

@ -43,10 +43,10 @@ namespace {
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool checkSegmentOverlap(uInt32 segmentBase1, uInt32 segmentSize1, uInt32 segmentBase2, uInt32 segmentSize2) { constexpr bool checkSegmentOverlap(uInt32 segmentBase1, uInt32 segmentSize1, uInt32 segmentBase2, uInt32 segmentSize2) {
return !(segmentBase1 + segmentSize1 <= segmentBase2 || segmentBase2 + segmentSize2 <= segmentBase1); return !(segmentBase1 + segmentSize1 <= segmentBase2 || segmentBase2 + segmentSize2 <= segmentBase1);
} }
} } // namespace
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
ElfLinker::ElfLinker(uInt32 textBase, uInt32 dataBase, uInt32 rodataBase, const ElfFile& elf) ElfLinker::ElfLinker(uInt32 textBase, uInt32 dataBase, uInt32 rodataBase, const ElfFile& elf)
@ -212,7 +212,7 @@ unique_ptr<uInt8[]>& ElfLinker::getSegmentDataRef(SegmentType type)
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void ElfLinker::relocateSections() void ElfLinker::relocateSections()
{ {
auto& sections = myElf.getSections(); const auto& sections = myElf.getSections();
myRelocatedSections.resize(sections.size(), std::nullopt); myRelocatedSections.resize(sections.size(), std::nullopt);
// relocate everything that is not .bss // relocate everything that is not .bss
@ -253,7 +253,7 @@ void ElfLinker::relocateSections()
ElfLinkError::raise("segments overlap"); ElfLinkError::raise("segments overlap");
// allocate segment data // allocate segment data
for (SegmentType segmentType: {SegmentType::text, SegmentType::data, SegmentType::rodata}) { for (const SegmentType segmentType: {SegmentType::text, SegmentType::data, SegmentType::rodata}) {
const uInt32 segmentSize = getSegmentSize(segmentType); const uInt32 segmentSize = getSegmentSize(segmentType);
if (segmentSize == 0) continue; if (segmentSize == 0) continue;
@ -293,7 +293,7 @@ void ElfLinker::relocateInitArrays()
std::unordered_map<uInt32, uInt32> relocatedPreinitArrays; std::unordered_map<uInt32, uInt32> relocatedPreinitArrays;
// Relocate init arrays // Relocate init arrays
for (size_t i = 0; i < sections.size(); i++) { for (uInt32 i = 0; i < sections.size(); i++) {
const auto& section = sections[i]; const auto& section = sections[i];
switch (section.type) { switch (section.type) {
@ -312,6 +312,9 @@ void ElfLinker::relocateInitArrays()
preinitArraySize += section.size; preinitArraySize += section.size;
break; break;
default:
break;
} }
} }
@ -369,7 +372,7 @@ void ElfLinker::relocateSymbols(const vector<ExternalSymbol>& externalSymbols)
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void ElfLinker::applyRelocationsToSections() void ElfLinker::applyRelocationsToSections()
{ {
auto& sections = myElf.getSections(); const auto& sections = myElf.getSections();
// apply relocations // apply relocations
for (size_t iSection = 0; iSection < sections.size(); iSection++) { for (size_t iSection = 0; iSection < sections.size(); iSection++) {
@ -383,7 +386,7 @@ void ElfLinker::applyRelocationsToSections()
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void ElfLinker::copyInitArrays(vector<uInt32>& initArray, std::unordered_map<uInt32, uInt32> relocatedInitArrays) void ElfLinker::copyInitArrays(vector<uInt32>& initArray, const std::unordered_map<uInt32, uInt32>& relocatedInitArrays)
{ {
const uInt8* elfData = myElf.getData(); const uInt8* elfData = myElf.getData();
const auto& sections = myElf.getSections(); const auto& sections = myElf.getSections();
@ -436,7 +439,7 @@ void ElfLinker::applyRelocationToSection(const ElfFile::Relocation& relocation,
{ {
const uInt32 op = read32(target); const uInt32 op = read32(target);
Int32 offset = relocatedSymbol->value + relocation.addend.value_or(elfUtil::decode_B_BL(op)) - const Int32 offset = relocatedSymbol->value + relocation.addend.value_or(elfUtil::decode_B_BL(op)) -
getSegmentBase(targetSectionRelocated.segment) - getSegmentBase(targetSectionRelocated.segment) -
targetSectionRelocated.offset - targetSectionRelocated.offset -
relocation.offset - 4; relocation.offset - 4;
@ -446,6 +449,9 @@ void ElfLinker::applyRelocationToSection(const ElfFile::Relocation& relocation,
write32(target, elfUtil::encode_B_BL(offset, relocation.type == ElfFile::R_ARM_THM_CALL)); write32(target, elfUtil::encode_B_BL(offset, relocation.type == ElfFile::R_ARM_THM_CALL));
} }
default:
break;
} }
} }
@ -456,7 +462,7 @@ void ElfLinker::applyRelocationsToInitArrays(uInt8 initArrayType, vector<uInt32>
const auto& symbols = myElf.getSymbols(); const auto& symbols = myElf.getSymbols();
const auto& sections = myElf.getSections(); const auto& sections = myElf.getSections();
for (size_t iSection = 0; iSection < sections.size(); iSection++) { for (uInt32 iSection = 0; iSection < sections.size(); iSection++) {
const auto& section = sections[iSection]; const auto& section = sections[iSection];
if (section.type != initArrayType) continue; if (section.type != initArrayType) continue;
@ -479,29 +485,9 @@ void ElfLinker::applyRelocationsToInitArrays(uInt8 initArrayType, vector<uInt32>
if (relocation.offset + 4 > section.size) if (relocation.offset + 4 > section.size)
ElfLinkError::raise("unable relocate init array: symbol " + relocation.symbolName + " out of range"); ElfLinkError::raise("unable relocate init array: symbol " + relocation.symbolName + " out of range");
const uInt32 index = (relocatedInitArrays.at(iSection) + relocation.offset) >> 2; const uInt32 index = (relocatedInitArrays.at(iSection) + relocation.offset) >> 2;
const uInt32 value = relocatedSymbol->value + relocation.addend.value_or(initArray[index]); const uInt32 value = relocatedSymbol->value + relocation.addend.value_or(initArray[index]);
initArray[index] = value | (symbols[relocation.symbol].type == ElfFile::STT_FUNC ? 1 : 0); initArray[index] = value | (symbols[relocation.symbol].type == ElfFile::STT_FUNC ? 1 : 0);
} }
} }
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
uInt32 ElfLinker::read32(const uInt8* address)
{
uInt32 value = *(address++);
value |= *(address++) << 8;
value |= *(address++) << 16;
value |= *(address++) << 24;
return value;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void ElfLinker::write32(uInt8* address, uInt32 value)
{
*(address++) = value;
*(address++) = value >> 8;
*(address++) = value >> 16;
*(address++) = value >> 24;
}

View File

@ -101,14 +101,26 @@ class ElfLinker {
void relocateInitArrays(); void relocateInitArrays();
void relocateSymbols(const vector<ExternalSymbol>& externalSymbols); void relocateSymbols(const vector<ExternalSymbol>& externalSymbols);
void applyRelocationsToSections(); void applyRelocationsToSections();
void copyInitArrays(vector<uInt32>& initArray, std::unordered_map<uInt32, uInt32> relocatedInitArrays); void copyInitArrays(vector<uInt32>& initArray, const std::unordered_map<uInt32, uInt32>& relocatedInitArrays);
void applyRelocationToSection(const ElfFile::Relocation& relocation, size_t iSection); void applyRelocationToSection(const ElfFile::Relocation& relocation, size_t iSection);
void applyRelocationsToInitArrays(uInt8 initArrayType, vector<uInt32>& initArray, void applyRelocationsToInitArrays(uInt8 initArrayType, vector<uInt32>& initArray,
const std::unordered_map<uInt32, uInt32>& relocatedInitArrays); const std::unordered_map<uInt32, uInt32>& relocatedInitArrays);
uInt32 read32(const uInt8* address); static constexpr uInt32 read32(const uInt8* address) {
void write32(uInt8* address, uInt32 value); uInt32 value = *(address++);
value |= *(address++) << 8;
value |= *(address++) << 16;
value |= *(address++) << 24;
return value;
}
static constexpr void write32(uInt8* address, uInt32 value) {
*(address++) = value;
*(address++) = value >> 8;
*(address++) = value >> 16;
*(address++) = value >> 24;
}
private: private:
std::optional<uInt32> undefinedSymbolDefault; std::optional<uInt32> undefinedSymbolDefault;

View File

@ -263,7 +263,7 @@ const char* ElfParser::getName(const Section& section, uInt32 offset) const
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
const ElfParser::Section* ElfParser::getSymtab() const const ElfParser::Section* ElfParser::getSymtab() const
{ {
for (auto& section: mySections) for (const auto& section: mySections)
if (section.type == SHT_SYMTAB) return &section; if (section.type == SHT_SYMTAB) return &section;
return nullptr; return nullptr;
@ -312,7 +312,7 @@ ostream& operator<<(ostream& os, const ElfParser::Section& section)
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
ostream& operator<<(ostream& os, const ElfParser::Symbol symbol) ostream& operator<<(ostream& os, const ElfParser::Symbol& symbol)
{ {
std::ios reset(nullptr); std::ios reset(nullptr);
reset.copyfmt(os); reset.copyfmt(os);
@ -324,8 +324,8 @@ ostream& operator<<(ostream& os, const ElfParser::Symbol symbol)
<< " value=0x" << std::setw(8) << symbol.value << " value=0x" << std::setw(8) << symbol.value
<< " size=0x" << std::setw(8) << symbol.size << " size=0x" << std::setw(8) << symbol.size
<< std::setw(1) << std::setw(1)
<< " bind=0x" << std::setw(2) << (int)symbol.bind << " bind=0x" << std::setw(2) << static_cast<int>(symbol.bind)
<< " type=0x" << std::setw(2) << (int)symbol.type; << " type=0x" << std::setw(2) << static_cast<int>(symbol.type);
os.copyfmt(reset); os.copyfmt(reset);
@ -335,7 +335,7 @@ ostream& operator<<(ostream& os, const ElfParser::Symbol symbol)
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
ostream& operator<<(ostream& os, const ElfParser::Relocation rel) ostream& operator<<(ostream& os, const ElfParser::Relocation& rel)
{ {
std::ios reset(nullptr); std::ios reset(nullptr);
reset.copyfmt(os); reset.copyfmt(os);
@ -345,14 +345,14 @@ ostream& operator<<(ostream& os, const ElfParser::Relocation rel)
<< std::hex << std::setfill('0') << std::hex << std::setfill('0')
<< " offset=0x" << std::setw(8) << rel.offset << " offset=0x" << std::setw(8) << rel.offset
<< " info=0x" << std::setw(8) << rel.info << " info=0x" << std::setw(8) << rel.info
<< " type=0x" << std::setw(2) << (int)rel.type; << " type=0x" << std::setw(2) << static_cast<int>(rel.type);
if (rel.addend.has_value()) if (rel.addend.has_value())
os << " addend=0x" << std::setw(8) << *rel.addend; os << " addend=0x" << std::setw(8) << *rel.addend;
os.copyfmt(reset); os.copyfmt(reset);
os << " symbol=" << (int)rel.symbol; os << " symbol=" << static_cast<int>(rel.symbol);
return os; return os;
} }

View File

@ -98,7 +98,7 @@ class ElfParser : public ElfFile {
}; };
ostream& operator<<(ostream& os, const ElfParser::Section& section); ostream& operator<<(ostream& os, const ElfParser::Section& section);
ostream& operator<<(ostream& os, const ElfParser::Symbol symbol); ostream& operator<<(ostream& os, const ElfParser::Symbol& symbol);
ostream& operator<<(ostream& os, const ElfParser::Relocation relocation); ostream& operator<<(ostream& os, const ElfParser::Relocation& relocation);
#endif // ELF_PARSER #endif // ELF_PARSER

View File

@ -21,8 +21,8 @@ Int32 elfUtil::decode_B_BL(uInt32 opcode)
{ {
// nomenclature follows Thumb32 BL / B.W encoding in Arm Architecture Reference // nomenclature follows Thumb32 BL / B.W encoding in Arm Architecture Reference
uInt16 hw1 = opcode; const uInt16 hw1 = opcode;
uInt16 hw2 = opcode >> 16; const uInt16 hw2 = opcode >> 16;
const uInt8 s = (hw1 >> 10) & 0x01; const uInt8 s = (hw1 >> 10) & 0x01;
const uInt8 i1 = ~((hw2 >> 13) ^ s) & 0x01; const uInt8 i1 = ~((hw2 >> 13) ^ s) & 0x01;
@ -44,13 +44,13 @@ uInt32 elfUtil::encode_B_BL(Int32 offset, bool link)
offset >>= 1; offset >>= 1;
uInt8 s = (offset >> 23) & 0x01; const uInt8 s = (offset >> 23) & 0x01;
uInt8 j2 = ((~offset >> 21) ^ s) & 0x01; const uInt8 j2 = ((~offset >> 21) ^ s) & 0x01;
uInt8 j1 = ((~offset >> 22) ^ s) & 0x01; const uInt8 j1 = ((~offset >> 22) ^ s) & 0x01;
uInt32 imm11 = offset & 0x7ff; const uInt32 imm11 = offset & 0x7ff;
uInt32 imm10 = (offset >> 11) & 0x3ff; const uInt32 imm10 = (offset >> 11) & 0x3ff;
uInt16 hw1 = 0xf000 | (s << 10) | imm10; const uInt16 hw1 = 0xf000 | (s << 10) | imm10;
uInt16 hw2 = 0x9000 | (j1 << 13) | (j2 << 11) | imm11; uInt16 hw2 = 0x9000 | (j1 << 13) | (j2 << 11) | imm11;
if (link) hw2 |= 0x4000; if (link) hw2 |= 0x4000;