@@ -113,15 +113,49 @@ const std::vector<std::string> cRelSectionMask = {
113
113
114
114
int main (int argc, char **argv)
115
115
{
116
- if (argc <= 2 )
116
+ std::string elfFilename;
117
+ std::string lstFilename;
118
+ std::string relFilename = " " ;
119
+ int moduleID = 33 ;
120
+
117
121
{
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
+ }
120
152
}
121
153
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
+
125
159
// Load input file
126
160
ELFIO::elfio inputElf;
127
161
if (!inputElf.load (elfFilename))
@@ -199,10 +233,13 @@ int main(int argc, char **argv)
199
233
for (const auto §ion : inputElf.sections )
200
234
{
201
235
// 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 )
206
243
{
207
244
// BSS?
208
245
if (section->get_type () == SHT_NOBITS)
@@ -252,9 +289,6 @@ int main(int argc, char **argv)
252
289
// Fill in section info in main buffer
253
290
std::copy (sectionInfoBuffer.begin (), sectionInfoBuffer.end (), outputBuffer.begin () + sectionInfoOffset);
254
291
255
- // #todo-elf2rel: Make this accessible via program options, configured for TTYD right now
256
- int moduleID = 33 ;
257
-
258
292
// Find all relocations
259
293
struct Relocation
260
294
{
@@ -315,6 +349,16 @@ int main(int argc, char **argv)
315
349
rel.moduleID = moduleID;
316
350
rel.targetSection = static_cast <uint8_t >(sectionIndex);
317
351
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
+ }
318
362
}
319
363
else
320
364
{
@@ -463,7 +507,7 @@ int main(int argc, char **argv)
463
507
case R_DOLPHIN_END:
464
508
break ;
465
509
default :
466
- printf (" Unsupported relocation type %d" , nextRel.type );
510
+ printf (" Unsupported relocation type %d\n " , nextRel.type );
467
511
break ;
468
512
}
469
513
@@ -494,7 +538,6 @@ int main(int argc, char **argv)
494
538
std::copy (headerBuffer.begin (), headerBuffer.end (), outputBuffer.begin ());
495
539
496
540
// Write final REL file
497
- std::string relFilename = elfFilename.substr (0 , elfFilename.find_last_of (' .' )) + " .rel" ;
498
541
std::ofstream outputStream (relFilename, std::ios::binary);
499
542
outputStream.write (reinterpret_cast <const char *>(outputBuffer.data ()), outputBuffer.size ());
500
543
0 commit comments