Skip to content

Commit e58d7ce

Browse files
PistonMinerZephiles
authored andcommitted
elf2rel: Add support for REL file format version 1 and 2
1 parent 9b18073 commit e58d7ce

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

ttyd-tools/elf2rel/elf2rel.cpp

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ std::map<std::string, uint32_t> loadSymbolMap(const std::string &filename)
3939
}
4040

4141
void writeModuleHeader(std::vector<uint8_t> &buffer,
42+
int version,
4243
int id,
4344
int sectionCount,
4445
int sectionInfoOffset,
@@ -63,7 +64,7 @@ void writeModuleHeader(std::vector<uint8_t> &buffer,
6364
save<uint32_t>(buffer, sectionInfoOffset);
6465
save<uint32_t>(buffer, 0); // name offset
6566
save<uint32_t>(buffer, 0); // name size
66-
save<uint32_t>(buffer, 3); // version
67+
save<uint32_t>(buffer, version); // version
6768

6869
save<uint32_t>(buffer, totalBssSize);
6970
save<uint32_t>(buffer, relocationOffset);
@@ -76,9 +77,15 @@ void writeModuleHeader(std::vector<uint8_t> &buffer,
7677
save<uint32_t>(buffer, prologOffset);
7778
save<uint32_t>(buffer, epilogOffset);
7879
save<uint32_t>(buffer, unresolvedOffset);
79-
save<uint32_t>(buffer, maxAlign);
80-
save<uint32_t>(buffer, maxBssAlign);
81-
save<uint32_t>(buffer, fixedDataSize);
80+
if (version >= 2)
81+
{
82+
save<uint32_t>(buffer, maxAlign);
83+
save<uint32_t>(buffer, maxBssAlign);
84+
}
85+
if (version >= 3)
86+
{
87+
save<uint32_t>(buffer, fixedDataSize);
88+
}
8289
}
8390

8491
void writeSectionInfo(std::vector<uint8_t> &buffer, int offset, int size)
@@ -117,6 +124,7 @@ int main(int argc, char **argv)
117124
std::string lstFilename;
118125
std::string relFilename = "";
119126
int moduleID = 33;
127+
int relVersion = 3;
120128

121129
{
122130
namespace po = boost::program_options;
@@ -127,7 +135,8 @@ int main(int argc, char **argv)
127135
("input-file,i", po::value(&elfFilename), "Input ELF filename (required)")
128136
("symbol-file,s", po::value(&lstFilename), "Input symbol file name (required)")
129137
("output-file,o", po::value(&relFilename), "Output REL filename")
130-
("rel-id", po::value(&moduleID)->default_value(0x1000), "REL file ID");
138+
("rel-id", po::value(&moduleID)->default_value(0x1000), "REL file ID")
139+
("rel-version", po::value(&relVersion)->default_value(3), "REL file format version (1, 2, 3)");
131140

132141
po::positional_options_description positionals;
133142
positionals.add("input-file", -1);
@@ -144,7 +153,9 @@ int main(int argc, char **argv)
144153

145154
if (varMap.count("help")
146155
|| varMap.count("input-file") != 1
147-
|| varMap.count("symbol-file") != 1)
156+
|| varMap.count("symbol-file") != 1
157+
|| relVersion < 1
158+
|| relVersion > 3)
148159
{
149160
std::cout << description << "\n";
150161
return 1;
@@ -217,7 +228,7 @@ int main(int argc, char **argv)
217228

218229
std::vector<uint8_t> outputBuffer;
219230
// Dummy values for header until offsets are determined
220-
writeModuleHeader(outputBuffer, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
231+
writeModuleHeader(outputBuffer, relVersion, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
221232
int sectionInfoOffset = outputBuffer.size();
222233
for (int i = 0; i < inputElf.sections.size(); ++i)
223234
{
@@ -523,6 +534,7 @@ int main(int argc, char **argv)
523534
// Write final header
524535
std::vector<uint8_t> headerBuffer;
525536
writeModuleHeader(headerBuffer,
537+
relVersion,
526538
moduleID,
527539
inputElf.sections.size(),
528540
sectionInfoOffset,

0 commit comments

Comments
 (0)