summaryrefslogtreecommitdiffhomepage
path: root/CompilerDriver
diff options
context:
space:
mode:
authorAmlal El Mahrouss <amlal.elmahrouss@icloud.com>2024-01-01 11:58:00 +0100
committerAmlal El Mahrouss <amlal.elmahrouss@icloud.com>2024-01-01 12:01:27 +0100
commit432698e9bc416e62fcb5618a9e5ae78032feaa76 (patch)
tree04cba4a008c3b4cf78c32a7975de33ee347c05a5 /CompilerDriver
parent4364174de7c2de38947f7108858aa47ffb3b296b (diff)
ld+masm: improved their logging abilities.
ld: breaking change, change origin address; also return an error when .bad() is true. masm: small improvements, abort on compilation error. Signed-off-by: Amlal El Mahrouss <amlal.elmahrouss@icloud.com>
Diffstat (limited to 'CompilerDriver')
-rw-r--r--CompilerDriver/ld.cxx64
-rw-r--r--CompilerDriver/masm.cxx38
2 files changed, 89 insertions, 13 deletions
diff --git a/CompilerDriver/ld.cxx b/CompilerDriver/ld.cxx
index 6972e2c..cfc664f 100644
--- a/CompilerDriver/ld.cxx
+++ b/CompilerDriver/ld.cxx
@@ -40,7 +40,7 @@
#define kWhite "\e[0;97m"
#define kStdOut (std::cout << kWhite)
-#define kPefDefaultStart 0x8000000
+#define kPefDeaultOrg (uint64_t)0x10000
#define kPefLinkerNumId 0x333D
#define kPefAbiId "Container:Abi:MP-UX"
@@ -87,8 +87,6 @@ int main(int argc, char** argv)
kStdOut << "-verbose: Print program backtrace (verbose mode).\n";
kStdOut << "-shared: Output as a shared library.\n";
kStdOut << "-m64000: Link for the X64000.\n";
- kStdOut << "-m88000: Link for the X88000.\n";
- kStdOut << "-mppc64: Link for the PowerPC.\n";
kStdOut << "-fatbin: Output as FAT PEF.\n";
kStdOut << "-o: Select output filename.\n";
@@ -99,7 +97,6 @@ int main(int argc, char** argv)
StringCompare(argv[i], "--version") == 0)
{
kStdOut << kToolVersion << std::endl;
-
// bye :D
return 0;
}
@@ -150,7 +147,7 @@ int main(int argc, char** argv)
continue;
}
- kStdOut << "ld: ignore: " << argv[i] << "\n";
+ kStdOut << "ld: ignore flag: " << argv[i] << "\n";
}
// sanity check.
@@ -192,12 +189,28 @@ int main(int argc, char** argv)
pef_container.Version = kPefVersion;
// specify the start address.
- pef_container.Start = kPefDefaultStart;
+ pef_container.Start = kPefDeaultOrg;
pef_container.HdrSz = sizeof(CxxKit::PEFContainer);
std::ofstream output_fc(kOutput, std::ofstream::binary);
+
+ if (output_fc.bad())
+ {
+ if (kVerbose)
+ {
+ kStdOut << "ld: error: " << strerror(errno) << "\n";
+ }
+
+ return -CXXKIT_FILE_NOT_FOUND;
+ }
+
output_fc << pef_container;
+ if (kVerbose)
+ {
+ kStdOut << "ld: PEF: wrote container header.\n";
+ }
+
//! Read AE to convert as PEF.
std::vector<CxxKit::PEFCommandHeader> pef_command_hdrs;
@@ -217,13 +230,26 @@ int main(int argc, char** argv)
if (ae_header.fArch != kArch)
{
+ if (kVerbose)
+ kStdOut << "ld: PEF: is a fat binary? : ";
+
if (!kFatBinaryEnable)
- {
+ {
+ if (kVerbose)
+ kStdOut << "NO\n";
+
kStdOut << "ld: error: object " << i << " is a different kind of architecture and output isn't treated as FAT binary." << std::endl;
std::remove(kOutput.c_str());
return -CXXKIT_FAT_ERROR;
}
+ else
+ {
+ if (kVerbose)
+ {
+ kStdOut << "YES\n";
+ }
+ }
}
if (ae_header.fMagic[0] == kAEMag0 &&
@@ -232,6 +258,9 @@ int main(int argc, char** argv)
{
std::size_t cnt = ae_header.fCount;
+ if (kVerbose)
+ kStdOut << "ld: AE: header found, record count: " << cnt << "\n";
+
pef_container.Count = cnt;
char* raw_ae_records = new char[cnt * sizeof(CxxKit::AERecordHeader)];
@@ -278,6 +307,9 @@ ld_mark_header:
command_header.Kind = ae_records[ae_record_index].fKind;
command_header.Size = ae_records[ae_record_index].fSize;
+ if (kVerbose)
+ kStdOut << "ld: AE: record: " << ae_records[ae_record_index].fName << " was marked.\n";
+
pef_command_hdrs.emplace_back(command_header);
}
@@ -319,6 +351,9 @@ ld_mark_header:
std::string(pef_command_hdr.Name).find(kLdDynamicSym) ==
std::string::npos)
{
+ if (kVerbose)
+ kStdOut << "ld: found undefined symbol: " << pef_command_hdr.Name << "\n";
+
if (auto it = std::find(not_found.begin(), not_found.end(), std::string(pef_command_hdr.Name));
it == not_found.end())
{
@@ -367,6 +402,9 @@ ld_mark_header:
not_found.erase(it);
+ if (kVerbose)
+ kStdOut << "ld: found symbol: " << pef_command_hdr.Name << "\n";
+
break;
}
}
@@ -380,6 +418,9 @@ ld_continue_search:
if (!kStartFound && is_executable)
{
+ if (kVerbose)
+ kStdOut << "ld: undefined symbol: __start, you may have forget to link agaisnt your runtime library.\n";
+
kStdOut << "ld: undefined entrypoint " << kPefStart << " for executable " << kOutput << "\n";
}
@@ -476,6 +517,9 @@ ld_continue_search:
duplicate_symbols.push_back(pef_command_hdr.Name);
}
+ if (kVerbose)
+ kStdOut << "ld: found duplicate symbol: " << pef_command_hdr.Name << "\n";
+
kDuplicateSymbols = true;
}
}
@@ -499,6 +543,9 @@ ld_continue_search:
output_fc << byte;
}
+ if (kVerbose)
+ kStdOut << "ld: wrote code for: " << kOutput << "\n";
+
// step 3: check if we have those symbols
std::vector<std::string> unreferenced_symbols;
@@ -525,6 +572,9 @@ ld_continue_search:
std::filesystem::exists(kOutput) ||
!unreferenced_symbols.empty())
{
+ if (kVerbose)
+ kStdOut << "ld: code for: " << kOutput << ", is corrupt, removing file...\n";
+
std::remove(kOutput.c_str());
return -CXXKIT_EXEC_ERROR;
}
diff --git a/CompilerDriver/masm.cxx b/CompilerDriver/masm.cxx
index 3ece592..41cd7c9 100644
--- a/CompilerDriver/masm.cxx
+++ b/CompilerDriver/masm.cxx
@@ -133,19 +133,15 @@ int main(int argc, char** argv)
kStdOut << "-v: Print program version.\n";
kStdOut << "-verbose: Print verbose output.\n";
kStdOut << "-m64000: Compile for the X64000 instruction set.\n";
- kStdOut << "-m68000: Compile for the NXP 68000 instruction set.\n";
- kStdOut << "-mppc64: Compile for the PowerPC instruction set.\n";
return 0;
}
-
- if (strcmp(argv[i], "-m64000") == 0)
+ else if (strcmp(argv[i], "-m64000") == 0)
{
kOutputArch = CxxKit::kPefArchARC;
continue;
}
-
- if (strcmp(argv[i], "-verbose") == 0)
+ else if (strcmp(argv[i], "-verbose") == 0)
{
kVerbose = true;
continue;
@@ -171,6 +167,14 @@ int main(int argc, char** argv)
std::ofstream file_ptr_out(object_output,
std::ofstream::binary);
+ if (file_ptr_out.bad())
+ {
+ if (kVerbose)
+ {
+ kStdOut << "masm: error: " << strerror(errno) << "\n";
+ }
+ }
+
std::string line;
CxxKit::AEHeader hdr{ 0 };
@@ -207,6 +211,12 @@ int main(int argc, char** argv)
}
catch(const std::exception& e)
{
+ if (kVerbose)
+ {
+ std::string what = e.what();
+ detail::print_warning("exit because of: " + what, "masm");
+ }
+
std::filesystem::remove(object_output);
goto masm_fail_exit;
}
@@ -605,6 +615,8 @@ static bool masm_write_number(std::size_t pos, std::string& jump_label)
if (errno != 0)
{
detail::print_error("invalid hex number: " + jump_label, "masm");
+ throw std::runtime_error("invalid_hex");
+
return false;
}
}
@@ -633,6 +645,8 @@ static bool masm_write_number(std::size_t pos, std::string& jump_label)
if (errno != 0)
{
detail::print_error("invalid binary number: " + jump_label, "masm");
+ throw std::runtime_error("invalid_bin");
+
return false;
}
}
@@ -661,6 +675,8 @@ static bool masm_write_number(std::size_t pos, std::string& jump_label)
if (errno != 0)
{
detail::print_error("invalid octal number: " + jump_label, "masm");
+ throw std::runtime_error("invalid_octal");
+
return false;
}
}
@@ -785,6 +801,7 @@ static void masm_read_instruction(std::string& line, const std::string& file)
if (found_some == 1)
{
detail::print_error("unrecognized register found.\ntip: each masm register starts with 'r'.\nline: " + line, file);
+ throw std::runtime_error("not_a_register");
}
}
@@ -795,12 +812,14 @@ static void masm_read_instruction(std::string& line, const std::string& file)
name != "stw")
{
detail::print_error("invalid combination of opcode and registers.\nline: " + line, file);
+ throw std::runtime_error("invalid_comb_op_reg");
}
if (found_some > 0 &&
name == "pop")
{
detail::print_error("invalid combination for opcode 'pop'.\ntip: it expects nothing.\nline: " + line, file);
+ throw std::runtime_error("invalid_comb_op_pop");
}
}
default:
@@ -845,6 +864,8 @@ static void masm_read_instruction(std::string& line, const std::string& file)
if (name == "sta")
{
detail::print_error("invalid combination of opcode and operands.\nhere ->" + line, file);
+ throw std::runtime_error("invalid_comb_op_ops");
+
break;
}
@@ -856,6 +877,7 @@ static void masm_read_instruction(std::string& line, const std::string& file)
cpy_jump_label.find("__import") != std::string::npos)
{
detail::print_error("invalid usage __import on 'sta', here: " + line, file);
+ throw std::runtime_error("invalid_sta_usage");
break;
}
}
@@ -872,12 +894,16 @@ masm_write_label:
name == "psh" ||
cpy_jump_label.find("__import") == std::string::npos &&
name == "jb")
+ {
detail::print_error("__import not found on jump label, please add one.", file.c_str());
+ throw std::runtime_error("import_jmp_lbl");
+ }
else if (cpy_jump_label.find("__import") != std::string::npos)
{
if (name == "sta")
{
detail::print_error("__import is not allowed on a sta operation.", file.c_str());
+ throw std::runtime_error("import_sta_op");
}
cpy_jump_label.erase(cpy_jump_label.find("__import"), strlen("__import"));