This commit is contained in:
nora 2024-11-29 23:10:22 +01:00
parent 741b1000b3
commit b2d506b2ae
4 changed files with 23 additions and 7 deletions

1
.gitignore vendored
View file

@ -1,2 +1,3 @@
a.out a.out
*.o *.o
.vscode

View file

@ -453,12 +453,14 @@ function lower(ast) {
const RELOCATIONS = { const RELOCATIONS = {
R_X86_64_PC32: 2, R_X86_64_PC32: 2,
R_X86_64_PLT32: 4,
}; };
const SYMBOL_TYPES = { const SYMBOL_TYPES = {
STT_NOTYPE: 0, STT_NOTYPE: 0,
STT_FUNC: 2, STT_FUNC: 2,
}; };
const SYMBOL_BINDING = { const SYMBOL_BINDING = {
STB_LOCAL: 0,
STB_GLOBAL: 1, STB_GLOBAL: 1,
}; };
const SYMBOL_VISIBILITY = { const SYMBOL_VISIBILITY = {
@ -653,7 +655,7 @@ function lower(ast) {
// ; 64-bits in 64-bit mode // ; 64-bits in 64-bit mode
this.#append([0xe8]); this.#append([0xe8]);
this.relocations.push({ this.relocations.push({
kind: RELOCATIONS.R_X86_64_PC32, kind: RELOCATIONS.R_X86_64_PC32, // pietro said i should use this even though PC_32 works too
symbol, symbol,
offset: this.out.length, offset: this.out.length,
addend: -4, addend: -4,
@ -844,7 +846,17 @@ function lower(ast) {
}; };
} }
const symbols = []; const symbols = [
{
name: "",
type: SYMBOL_TYPES.STT_NOTYPE,
binding: SYMBOL_BINDING.STB_LOCAL,
visibility: SYMBOL_VISIBILITY.STV_DEFAULT,
sectionIndex: 0,
value: 0,
size: 0,
},
];
const { const {
textContent, textContent,
@ -1045,6 +1057,10 @@ function lower(ast) {
// symtab section // symtab section
const strTableIndex = sectionCount + 1; const strTableIndex = sectionCount + 1;
const firstGlobal = symbols.findIndex(
(sym) => sym.binding === SYMBOL_BINDING.STB_GLOBAL
);
assertDefined(firstGlobal);
writeSectionHeader(".symtab", { writeSectionHeader(".symtab", {
type: /*SHT_SYMTAB*/ 2, type: /*SHT_SYMTAB*/ 2,
flags: 0, flags: 0,
@ -1052,7 +1068,7 @@ function lower(ast) {
offset: 0, offset: 0,
size: symtab.buffer.length, size: symtab.buffer.length,
link: strTableIndex, link: strTableIndex,
info: 0, info: firstGlobal,
addralign: 8, addralign: 8,
entsize: 24, entsize: 24,
}); });
@ -1159,7 +1175,7 @@ async function link(object) {
const outputFile = "output.o"; const outputFile = "output.o";
fs.writeFile(outputFile, object); fs.writeFile(outputFile, object);
await execWithForwardedOutput("gcc", [outputFile]); await execWithForwardedOutput("gcc", ["-Wl,-znoexecstack", outputFile]);
await execWithForwardedOutput("gdb", [ await execWithForwardedOutput("gdb", [
"--batch", "--batch",
"--command", "--command",

View file

@ -3,8 +3,7 @@
int main(int argc, int argv) int main(int argc, int argv)
{ {
int x = 100; int x = 100;
thisismyfakeconstantbecauseidonthaveconstant(x - 1); return thisismyfakeconstantbecauseidonthaveconstant(x - 1);
return argv;
} }
int thisismyfakeconstantbecauseidonthaveconstant(int x) int thisismyfakeconstantbecauseidonthaveconstant(int x)

View file

@ -1,3 +1,3 @@
{ pkgs ? import <nixpkgs> { } }: pkgs.mkShell { { pkgs ? import <nixpkgs> { } }: pkgs.mkShell {
nativeBuildInputs = with pkgs; [ nodejs_22 llvmPackages_18.lld ]; nativeBuildInputs = with pkgs; [ nodejs_22 llvmPackages_18.lld llvmPackages_18.clang ];
} }