Skip to content

Commit

Permalink
use linker script
Browse files Browse the repository at this point in the history
  • Loading branch information
MishaProductions committed Dec 3, 2023
1 parent 8ffeaf7 commit c2c1395
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 21 deletions.
101 changes: 91 additions & 10 deletions source/Cosmos.Build.Tasks/Ld.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ public class Ld : ToolTask

public string DataAddress { get; set; }

public string BssAddress { get; set; }
public string BssAddress { get; set; }

[Required]
public string TargetArchitecture { get; set; }

#endregion

Expand Down Expand Up @@ -131,17 +134,95 @@ protected override string GenerateCommandLineCommands()
{
CommandLineBuilder xBuilder = new();

xBuilder.AppendSwitchIfNotNull("-Ttext ", TextAddress);
xBuilder.AppendSwitchIfNotNull("-Tdata ", DataAddress);
xBuilder.AppendSwitchIfNotNull("-Tbss ", BssAddress);
xBuilder.AppendSwitchIfNotNull("-e ", Entry);
xBuilder.AppendSwitchIfNotNull("-o ", OutputFile);

xBuilder.AppendFileNamesIfNotNull(InputFiles, " ");
xBuilder.AppendSwitch("-m elf_x86_64");

Log.LogMessage(MessageImportance.High, xBuilder.ToString());

xBuilder.AppendFileNamesIfNotNull(InputFiles, " ");

if (TargetArchitecture == "amd64")
{
var dir = Path.GetDirectoryName(OutputFile);
var path = dir + "/linker.ld";
xBuilder.AppendSwitch("-m elf_x86_64");
xBuilder.AppendSwitchIfNotNull("-T ", path);



File.WriteAllText(path, @"/* Tell the linker that we want an x86_64 ELF64 output file */
OUTPUT_FORMAT(elf64-x86-64)
OUTPUT_ARCH(i386:x86-64)
/* We want the symbol _start to be our entry point */
ENTRY(" + Entry + @")
/* Define the program headers we want so the bootloader gives us the right */
/* MMU permissions */
PHDRS
{
text PT_LOAD FLAGS((1 << 0) | (1 << 2)) ; /* Execute + Read */
rodata PT_LOAD FLAGS((1 << 2)) ; /* Read only */
data PT_LOAD FLAGS((1 << 1) | (1 << 2)) ; /* Write + Read */
dynamic PT_DYNAMIC FLAGS((1 << 1) | (1 << 2)) ; /* Dynamic PHDR for relocations */
}
SECTIONS
{
/* We wanna be placed in the topmost 2GiB of the address space, for optimisations */
/* and because that is what the Limine spec mandates. */
/* Any address in this region will do, but often 0xffffffff80000000 is chosen as */
/* that is the beginning of the region. */
. = 0xffffffff80000000;
.text : {
*(.text .text.*)
} :text
/* Move to the next memory page for .rodata */
. += CONSTANT(MAXPAGESIZE);
.rodata : {
*(.rodata .rodata.*)
} :rodata
/* Move to the next memory page for .data */
. += CONSTANT(MAXPAGESIZE);
.data : {
*(.data .data.*)
} :data
/* Dynamic section for relocations, both in its own PHDR and inside data PHDR */
.dynamic : {
*(.dynamic)
} :data :dynamic
/* NOTE: .bss needs to be the last thing mapped to :data, otherwise lots of */
/* unnecessary zeros will be written to the binary. */
/* If you need, for example, .init_array and .fini_array, those should be placed */
/* above this. */
.bss : {
*(.bss .bss.*)
*(COMMON)
} :data
/* Discard .note.* and .eh_frame since they may cause issues on some hosts. */
/DISCARD/ : {
*(.eh_frame)
*(.note .note.*)
}
}");
}
else
{
xBuilder.AppendSwitch("-m elf_i386");

xBuilder.AppendSwitchIfNotNull("-Ttext ", TextAddress);
xBuilder.AppendSwitchIfNotNull("-Tdata ", DataAddress);
xBuilder.AppendSwitchIfNotNull("-Tbss ", BssAddress);
xBuilder.AppendSwitchIfNotNull("-e ", Entry);
}

Log.LogMessage(MessageImportance.High, xBuilder.ToString());

return xBuilder.ToString();
}

Expand Down
25 changes: 16 additions & 9 deletions source/Cosmos.Build.Tasks/Nasm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ enum OutputFormatEnum
{
Bin,
ELF
}

}

#region Task Parameters

[Required]
public string InputFile { get; set; }

Expand All @@ -30,7 +30,10 @@ public string OutputFormat
set => mOutputFormat = (OutputFormatEnum)Enum.Parse(typeof(OutputFormatEnum), value, true);
}

public string OptimizationLevel { get; set; }
public string OptimizationLevel { get; set; }

[Required]
public string TargetArchitecture { get; set; }

#endregion

Expand Down Expand Up @@ -99,11 +102,15 @@ protected override string GenerateCommandLineCommands()
{
xBuilder.AppendSwitch("-dBIN_COMPILATION");
}
xBuilder.AppendSwitch("-m amd64");

/* Apply the optimization level that the user chose */
if(!String.IsNullOrWhiteSpace(OptimizationLevel) && !String.IsNullOrWhiteSpace(OptimizationLevel))
xBuilder.AppendSwitch($"-O{OptimizationLevel}");

if (TargetArchitecture == "amd64")
{
xBuilder.AppendSwitch("-m amd64");
}

/* Apply the optimization level that the user chose */
if (!String.IsNullOrWhiteSpace(OptimizationLevel) && !String.IsNullOrWhiteSpace(OptimizationLevel))
xBuilder.AppendSwitch($"-O{OptimizationLevel}");

xBuilder.AppendFileNameIfNotNull(InputFile);

Expand Down
6 changes: 4 additions & 2 deletions source/Cosmos.Build.Tasks/build/Cosmos.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,8 @@
OutputFormat="$(BinFormat)"
ToolPath="$(NasmToolPath)"
ToolExe="$(NasmToolExe)"
OptimizationLevel="$(OptimizationLevel)"/>
OptimizationLevel="$(OptimizationLevel)"
TargetArchitecture="$(TargetArchitecture)"/>

</Target>

Expand All @@ -255,7 +256,8 @@
DataAddress="0x1000000"
Entry="Kernel_Start"
ToolPath="$(LdToolPath)"
ToolExe="$(LdToolExe)" />
ToolExe="$(LdToolExe)"
TargetArchitecture="$(TargetArchitecture)"/>

</Target>

Expand Down

0 comments on commit c2c1395

Please sign in to comment.