Skip to content

Commit

Permalink
Initial import
Browse files Browse the repository at this point in the history
This changeset introduces the initial build infrastructure that makes it
possible to build a statically linked ELF binary with multiboot header
for the i686-unknown-linux-gnu target.
  • Loading branch information
jermar committed Mar 1, 2017
0 parents commit 0c673c8
Show file tree
Hide file tree
Showing 6 changed files with 218 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .cargo/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[target.i686-unknown-linux-gnu]
linker = "tools/lw.py"
14 changes: 14 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "deuteros"
version = "0.1.0"
authors = ["Jakub Jermář <jakub@jermar.eu>"]

[dependencies]
rlibc = "*"

33 changes: 33 additions & 0 deletions link.ld
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
ENTRY(start)

SECTIONS {
.unmapped 0x108000: AT (0) {
hardcoded_unmapped_load_address = .;
unmapped_ktext_start = .;
KEEP(*(multiboot));
*(K_TEXT_START);
unmapped_ktext_end = .;

unmapped_kdata_start = .;
*(K_DATA_START);
unmapped_kdata_end = .;
}

.mapped 0x80108000 + SIZEOF(.unmapped): AT (SIZEOF(.unmapped)) {
ktext_start = .;
*(.text .text.*);
ktext_end = .;

kdata_start = .;
*(.data);
*(.rodata .rodata.*);
*(COMMON);
*(.bss);
kdata_end = .;
}

/DISCARD/ : {
*(*);
}

}
91 changes: 91 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Copyright 2017 Jakub Jermář
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software
// and associated documentation files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
// BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

#![feature(lang_items)]
#![feature(const_fn)]
#![feature(associated_consts)]
#![feature(naked_functions)]
#![feature(asm)]

#![no_std]
#![no_main]

extern crate rlibc;

extern "C" {
static hardcoded_unmapped_load_address: u32;
}

type EntryPoint = extern "C" fn() -> !;

#[repr(C)]
pub struct MultibootHeader {
magic: i32,
flags: i32,
checksum: i32,
header: &'static MultibootHeader,
load_adr: &'static u32,
load_end_adr: u32,
bss_end_adr: u32,
entry: EntryPoint,
}

impl MultibootHeader {
const MULTIBOOT_HEADER_MAGIC: i32 = 0x1badb002;
const MULTIBOOT_HEADER_FLAGS: i32 = 0x00010003;

const fn new(myself: &'static Self, ladr: &'static u32, entry: EntryPoint) -> Self {
MultibootHeader {
magic: Self::MULTIBOOT_HEADER_MAGIC,
flags: Self::MULTIBOOT_HEADER_FLAGS,
checksum: -(Self::MULTIBOOT_HEADER_MAGIC + Self::MULTIBOOT_HEADER_FLAGS),
header: myself,
load_adr: ladr,
load_end_adr: 0,
bss_end_adr: 0,
entry: entry,
}
}
}

#[link_section = "multiboot"]
#[no_mangle]
pub static MULTIBOOT_HEADER: MultibootHeader =
MultibootHeader::new(&MULTIBOOT_HEADER,
unsafe { &hardcoded_unmapped_load_address },
start);

#[link_section = "K_TEXT_START"]
#[naked]
#[no_mangle]
pub extern "C" fn start() -> ! {
unsafe {
asm!("
cli\n
cld\n
");
}
loop {}
}

#[lang = "eh_personality"]
extern "C" fn eh_personality() {}

#[lang = "panic_fmt"]
fn panic_fmt() -> ! {
loop {}
}
70 changes: 70 additions & 0 deletions tools/lw.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2017 Jakub Jermář
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# - Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# - Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# - The name of the author may not be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#

#
# This script collects the essential fragments of the linker script command
# line while throwing away the rest and then executes the actual linker with
# the collected arguments plus the arguments we want to use for linking the
# kernel.
#

"""
Linker wrapper for linking Deuteros
"""

import sys;
import subprocess;

def collect():

cmd = []

need_next = False

for arg in sys.argv:
if need_next:
cmd = cmd + [arg]
need_next = False
elif arg == "-L" or arg == "-o":
cmd = cmd + [arg]
need_next = True
elif arg[-2:] == ".o" or arg[-5:] == ".rlib":
cmd = cmd + [arg]

return cmd

def link(args):
cmdline = " ".join(args)
subprocess.call("ld -m elf_i386 --gc-sections -n -Tlink.ld %s" % cmdline,
shell = True)

args = collect()
link(args)

0 comments on commit 0c673c8

Please sign in to comment.