Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PageAlreadyMapped: Failed to load code #1400

Open
abrassel opened this issue Mar 4, 2025 · 6 comments
Open

PageAlreadyMapped: Failed to load code #1400

abrassel opened this issue Mar 4, 2025 · 6 comments

Comments

@abrassel
Copy link

abrassel commented Mar 4, 2025

I have the following program:

#![no_std]
#![no_main]

use core::panic::PanicInfo;

static HELLO: &[u8] = b"Hello World!";

#[panic_handler]
fn panic(_: &PanicInfo) -> ! {
    loop {}
}

#[no_mangle]
pub extern "C" fn _start() -> ! {
    let vga_buffer = 0xb8000 as *mut u8;
    for (i, &byte) in HELLO.iter().enumerate() {
        unsafe {
            *vga_buffer.offset(i as isize * 2) = byte;
            *vga_buffer.offset(i as isize * 2 + 1) = 0xb;
        }
    }

    loop {}
}

I run it with

qemu-system-x86_64 -drive format=raw,file=target
/x86_64-blog-os/debug/bootimage-phil-os.bi

and get the following error:

Image

Failed to map page... PageAlreadyMapped

From looking around, it seems like this error is related to having two "main" functions. But I don't think that that is the case for me.

@bjorn3
Copy link
Contributor

bjorn3 commented Mar 4, 2025

What are the .cargo/config.toml, Cargo.toml and the cargo invocation you used to build it?

@abrassel
Copy link
Author

abrassel commented Mar 4, 2025

https://github.com/abrassel/phil-os is the exact source.

@bjorn3
Copy link
Contributor

bjorn3 commented Mar 5, 2025

Maybe try with the following target json file?

{
    "llvm-target": "x86_64-unknown-linux-gnu",
    "data-layout": "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128",
    "arch": "x86_64",
    "target-endian": "little",
    "target-pointer-width": "64",
    "target-c-int-width": "32",
    "os": "linux",
    "executables": true,
    "linker-flavor": "gcc",
    "pre-link-args": ["-m64"],
    "morestack": false
}

This matches https://os.phil-opp.com/minimal-rust-kernel/#target-specification

@abrassel
Copy link
Author

abrassel commented Mar 7, 2025

Interestingly, rustc complains about linker-flavor.

error: failed to run `rustc` to learn about target-specific information

Caused by:
  process didn't exit successfully: `/home/abrassel/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/rustc - --crate-name ___ --print=file-names --target /home/abrassel/Documents/phil-os/x86_64-blog-os.json --crate-type bin --crate-type rlib --crate-type dylib --crate-type cdylib --crate-type staticlib --crate-type proc-macro --print=sysroot --print=split-debuginfo --print=crate-name --print=cfg -Wwarnings` (exit status: 1)
  --- stderr
  error: Error loading target specification: pre-link-args: expected a JSON object with fields per linker-flavor.. Run `rustc --print target-list` for a list of built-in targets

@abrassel
Copy link
Author

abrassel commented Mar 7, 2025

I did this instead (dropping the -m64 flag) and adding a couple of other required fields:

{
    "llvm-target": "x86_64-unknown-linux-gnu",
    "data-layout": "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128",
    "arch": "x86_64",
    "target-endian": "little",
    "target-pointer-width": "64",
    "target-c-int-width": "32",
    "os": "linux",
    "executables": true,
    "linker-flavor": "gcc",
    "panic-strategy": "abort"
}

now, the complaint is:

error: linking with `cc` failed: exit status: 1
  |
  = note:  "cc" "/tmp/rustclYSL1G/symbols.o" "<5 object files omitted>" "-Wl,--as-needed" "-Wl,-Bstatic" "/home/abrassel/Documents/phil-os/target/x86_64-blog-os/debug/deps/{librustc_std_workspace_core-498ee787797a7283.rlib,libcore-0114943c562e945d.rlib,libcompiler_builtins-055b757525724935.rlib}.rlib" "-Wl,-Bdynamic" "-Wl,--eh-frame-hdr" "-Wl,-z,noexecstack" "-L" "<sysroot>/lib/rustlib/x86_64-blog-os/lib" "-o" "/home/abrassel/Documents/phil-os/target/x86_64-blog-os/debug/deps/phil_os-621cb2ffa6c28bdb" "-Wl,--gc-sections" "-no-pie" "-nodefaultlibs"
  = note: some arguments are omitted. use `--verbose` to show all linker arguments
  = note: /usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/11/../../../x86_64-linux-gnu/crt1.o: in function `_start':
          (.text+0x21): undefined reference to `__libc_start_main'
          collect2: error: ld returned 1 exit status

@abrassel
Copy link
Author

abrassel commented Mar 7, 2025

excellent, I permuted a bit more. I changed the linking flags, and got it working after I did the following:

{
    "llvm-target": "x86_64-unknown-linux-gnu",
    "data-layout": "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128",
    "arch": "x86_64",
    "target-endian": "little",
    "target-pointer-width": "64",
    "target-c-int-width": "32",
    "os": "linux",
    "executables": true,
    "linker": "rust-lld",
    "linker-flavor": "ld.lld",
    "panic-strategy": "abort"
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants