Skip to content

Commit 578efc2

Browse files
PistonMinerZephiles
authored andcommitted
elf2rel: Improve command line interaction
1 parent 3356e78 commit 578efc2

File tree

1 file changed

+58
-15
lines changed

1 file changed

+58
-15
lines changed

ttyd-tools/elf2rel/elf2rel.cpp

Lines changed: 58 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,49 @@ const std::vector<std::string> cRelSectionMask = {
113113

114114
int main(int argc, char **argv)
115115
{
116-
if (argc <= 2)
116+
std::string elfFilename;
117+
std::string lstFilename;
118+
std::string relFilename = "";
119+
int moduleID = 33;
120+
117121
{
118-
printf("Usage: %s <elf file> <symbol file>\n", argv[0]);
119-
return 1;
122+
namespace po = boost::program_options;
123+
124+
po::options_description description("Options");
125+
description.add_options()
126+
("help", "Print help message")
127+
("input-file,i", po::value(&elfFilename), "Input ELF filename (required)")
128+
("symbol-file,s", po::value(&lstFilename), "Input symbol file name (required)")
129+
("output-file,o", po::value(&relFilename), "Output REL filename")
130+
("rel-id", po::value(&moduleID)->default_value(0x1000), "REL file ID");
131+
132+
po::positional_options_description positionals;
133+
positionals.add("input-file", -1);
134+
135+
po::variables_map varMap;
136+
po::store(
137+
po::command_line_parser(argc, argv)
138+
.options(description)
139+
.positional(positionals)
140+
.run(),
141+
varMap
142+
);
143+
po::notify(varMap);
144+
145+
if (varMap.count("help")
146+
|| varMap.count("input-file") != 1
147+
|| varMap.count("symbol-file") != 1)
148+
{
149+
std::cout << description << "\n";
150+
return 1;
151+
}
120152
}
121153

122-
std::string elfFilename = argv[1];
123-
std::string lstFilename = argv[2];
124-
154+
if (relFilename == "")
155+
{
156+
relFilename = elfFilename.substr(0, elfFilename.find_last_of('.')) + ".rel";
157+
}
158+
125159
// Load input file
126160
ELFIO::elfio inputElf;
127161
if (!inputElf.load(elfFilename))
@@ -199,10 +233,13 @@ int main(int argc, char **argv)
199233
for (const auto &section : inputElf.sections)
200234
{
201235
// Should keep?
202-
if (std::find(cRelSectionMask.begin(),
203-
cRelSectionMask.end(),
204-
section->get_name()) != cRelSectionMask.end()
205-
&& section->get_size() != 0)
236+
if (std::find_if(cRelSectionMask.begin(),
237+
cRelSectionMask.end(),
238+
[&](const std::string &val)
239+
{
240+
return val == section->get_name()
241+
|| section->get_name().find(val + ".") == 0;
242+
}) != cRelSectionMask.end() && section->get_size() != 0)
206243
{
207244
// BSS?
208245
if (section->get_type() == SHT_NOBITS)
@@ -252,9 +289,6 @@ int main(int argc, char **argv)
252289
// Fill in section info in main buffer
253290
std::copy(sectionInfoBuffer.begin(), sectionInfoBuffer.end(), outputBuffer.begin() + sectionInfoOffset);
254291

255-
// #todo-elf2rel: Make this accessible via program options, configured for TTYD right now
256-
int moduleID = 33;
257-
258292
// Find all relocations
259293
struct Relocation
260294
{
@@ -315,6 +349,16 @@ int main(int argc, char **argv)
315349
rel.moduleID = moduleID;
316350
rel.targetSection = static_cast<uint8_t>(sectionIndex);
317351
rel.addend = static_cast<uint32_t>(addend + symbolValue);
352+
353+
ELFIO::section *targetSection = inputElf.sections[rel.targetSection];
354+
if (writtenSections.find(targetSection) == writtenSections.end() && targetSection->get_type() != SHT_NOBITS)
355+
{
356+
printf("Relocation from section '%s' offset %llx against symbol '%s' in unwritten section '%s'\n",
357+
relocatedSection->get_name().c_str(),
358+
offset,
359+
symbolName.c_str(),
360+
targetSection->get_name().c_str());
361+
}
318362
}
319363
else
320364
{
@@ -463,7 +507,7 @@ int main(int argc, char **argv)
463507
case R_DOLPHIN_END:
464508
break;
465509
default:
466-
printf("Unsupported relocation type %d", nextRel.type);
510+
printf("Unsupported relocation type %d\n", nextRel.type);
467511
break;
468512
}
469513

@@ -494,7 +538,6 @@ int main(int argc, char **argv)
494538
std::copy(headerBuffer.begin(), headerBuffer.end(), outputBuffer.begin());
495539

496540
// Write final REL file
497-
std::string relFilename = elfFilename.substr(0, elfFilename.find_last_of('.')) + ".rel";
498541
std::ofstream outputStream(relFilename, std::ios::binary);
499542
outputStream.write(reinterpret_cast<const char *>(outputBuffer.data()), outputBuffer.size());
500543

0 commit comments

Comments
 (0)