2017-09-23 01:56:37 +00:00
|
|
|
/*
|
|
|
|
Copyright 2017 Wolfgang Thaller.
|
|
|
|
|
|
|
|
This file is part of Retro68.
|
|
|
|
|
|
|
|
Retro68 is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Retro68 is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Retro68. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Elf2Mac.h"
|
2017-09-26 22:30:06 +00:00
|
|
|
#include "SegmentMap.h"
|
2017-09-23 01:56:37 +00:00
|
|
|
|
|
|
|
#include <gelf.h>
|
|
|
|
|
|
|
|
#include <err.h>
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
2017-09-28 23:43:54 +00:00
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
2017-09-23 01:56:37 +00:00
|
|
|
|
|
|
|
#include <boost/algorithm/string/predicate.hpp>
|
2017-09-28 23:43:54 +00:00
|
|
|
|
|
|
|
#include "Object.h"
|
2017-09-23 01:56:37 +00:00
|
|
|
|
|
|
|
using std::string;
|
|
|
|
using std::vector;
|
|
|
|
using std::ofstream;
|
2017-09-24 19:25:58 +00:00
|
|
|
|
2017-09-23 01:56:37 +00:00
|
|
|
string argvZero;
|
|
|
|
|
|
|
|
void RealLD(vector<string> args)
|
|
|
|
{
|
|
|
|
vector<const char*> argv;
|
|
|
|
string realLD = argvZero + ".real";
|
|
|
|
argv.push_back(realLD.c_str());
|
|
|
|
for(string& s : args)
|
|
|
|
argv.push_back(s.c_str());
|
|
|
|
argv.push_back(NULL);
|
|
|
|
|
|
|
|
pid_t pid = fork();
|
|
|
|
if(pid < 0)
|
|
|
|
{
|
|
|
|
perror("unable to fork");
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
else if(pid == 0)
|
|
|
|
{
|
|
|
|
execvp(argv[0], const_cast<char* const *> (argv.data()));
|
|
|
|
perror("exec failed");
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int wstatus;
|
|
|
|
int result = 0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
result = waitpid(pid, &wstatus, 0);
|
|
|
|
} while(result == -1 && errno == EINTR);
|
|
|
|
if(!WIFEXITED(wstatus))
|
|
|
|
{
|
|
|
|
errx(EXIT_FAILURE, "ld process did not exit properly");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int exitcode = WEXITSTATUS(wstatus);
|
|
|
|
if(exitcode)
|
|
|
|
exit(exitcode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
vector<string> args;
|
|
|
|
std::copy(argv + 1, argv+argc, std::back_inserter(args));
|
|
|
|
argvZero = argv[0];
|
|
|
|
|
|
|
|
if(boost::algorithm::ends_with(argv[0], "ld"))
|
|
|
|
{
|
|
|
|
string outputFile = "a.out";
|
|
|
|
bool elf2mac = false;
|
2017-09-24 13:40:19 +00:00
|
|
|
bool flatoutput = false;
|
2017-09-26 00:38:05 +00:00
|
|
|
bool segments = true;
|
2017-09-29 20:04:11 +00:00
|
|
|
bool stripMacsbug = false;
|
2017-09-23 01:56:37 +00:00
|
|
|
|
|
|
|
vector<string> args2;
|
|
|
|
for(auto p = args.begin(), e = args.end(); p != e; ++p)
|
|
|
|
{
|
|
|
|
if(*p == "-o")
|
|
|
|
{
|
|
|
|
++p;
|
|
|
|
if(p == e)
|
|
|
|
errx(EXIT_FAILURE, "-o missing argument");
|
|
|
|
outputFile = *p;
|
|
|
|
}
|
|
|
|
else if(boost::algorithm::starts_with(*p, "-o"))
|
|
|
|
{
|
|
|
|
outputFile = (*p).substr(2);
|
|
|
|
}
|
|
|
|
else if(*p == "-elf2mac")
|
|
|
|
{
|
|
|
|
elf2mac = true;
|
|
|
|
}
|
2017-09-24 13:40:19 +00:00
|
|
|
else if(*p == "--mac-flat")
|
|
|
|
{
|
|
|
|
elf2mac = true;
|
|
|
|
flatoutput = true;
|
2017-09-26 22:30:06 +00:00
|
|
|
segments = false;
|
2017-09-24 13:40:19 +00:00
|
|
|
}
|
2017-09-29 20:17:50 +00:00
|
|
|
else if(*p == "--mac-single")
|
|
|
|
{
|
|
|
|
elf2mac = true;
|
|
|
|
flatoutput = false;
|
|
|
|
segments = false;
|
|
|
|
}
|
2017-09-24 18:03:01 +00:00
|
|
|
else if(*p == "--mac-segments")
|
|
|
|
{
|
|
|
|
elf2mac = true;
|
|
|
|
if(flatoutput)
|
|
|
|
errx(EXIT_FAILURE, "--mac-segments can't be used with --mac-flat");
|
|
|
|
++p;
|
|
|
|
if(p == e)
|
|
|
|
errx(EXIT_FAILURE, "--mac-segments missing argument");
|
|
|
|
//segmentMapFile = *p;
|
|
|
|
}
|
2017-09-29 20:04:11 +00:00
|
|
|
else if(*p == "--mac-strip-macsbug")
|
|
|
|
{
|
|
|
|
stripMacsbug = true;
|
|
|
|
}
|
2017-09-23 01:56:37 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
args2.push_back(*p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(elf2mac)
|
|
|
|
{
|
|
|
|
char tmpfile[] = "/tmp/ldscriptXXXXXX";
|
|
|
|
int fd = mkstemp(tmpfile);
|
|
|
|
if(fd < 0)
|
|
|
|
errx(EXIT_FAILURE, "can't create temp file");
|
2017-09-24 19:25:58 +00:00
|
|
|
|
2017-09-28 23:43:54 +00:00
|
|
|
SegmentMap segmentMap;
|
2017-09-23 01:56:37 +00:00
|
|
|
{
|
|
|
|
ofstream out(tmpfile);
|
2017-09-26 22:30:06 +00:00
|
|
|
if(segments)
|
|
|
|
{
|
2017-09-29 20:04:11 +00:00
|
|
|
segmentMap.CreateLdScript(out, stripMacsbug);
|
2017-09-26 22:30:06 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-09-29 20:04:11 +00:00
|
|
|
CreateFlatLdScript(out, stripMacsbug);
|
2017-09-26 22:30:06 +00:00
|
|
|
}
|
2017-09-23 01:56:37 +00:00
|
|
|
}
|
2017-09-24 19:25:58 +00:00
|
|
|
|
2017-09-23 01:56:37 +00:00
|
|
|
args2.push_back("-o");
|
|
|
|
args2.push_back(outputFile + ".gdb");
|
|
|
|
args2.push_back("-T");
|
|
|
|
args2.push_back(tmpfile);
|
|
|
|
RealLD(args2);
|
|
|
|
unlink(tmpfile);
|
2017-09-28 23:43:54 +00:00
|
|
|
Object theObject(outputFile + ".gdb");
|
2017-09-24 13:40:19 +00:00
|
|
|
if(flatoutput)
|
2017-09-28 23:43:54 +00:00
|
|
|
theObject.FlatCode(outputFile);
|
2017-09-24 19:25:58 +00:00
|
|
|
else if(segments)
|
2017-09-28 23:43:54 +00:00
|
|
|
theObject.MultiSegmentApp(outputFile, segmentMap);
|
2017-09-24 13:40:19 +00:00
|
|
|
else
|
2017-09-28 23:43:54 +00:00
|
|
|
theObject.SingleSegmentApp(outputFile);
|
2017-09-23 01:56:37 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
RealLD(args);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(argc != 2)
|
|
|
|
errx(EXIT_FAILURE, "usage : %s file-name ", argv[0]);
|
2017-09-28 23:43:54 +00:00
|
|
|
Object theObject(argv[1]);
|
|
|
|
SegmentMap segmentMap;
|
|
|
|
theObject.MultiSegmentApp("out.bin", segmentMap);
|
2017-09-23 01:56:37 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|