Initial commit
This commit is contained in:
27
.editorconfig
Normal file
27
.editorconfig
Normal file
@@ -0,0 +1,27 @@
|
||||
[*]
|
||||
end_of_line = LF
|
||||
charset = utf-8
|
||||
indent_style = space
|
||||
indent_size = 4
|
||||
insert_final_newline = true
|
||||
trim_trailing_whitespace = true
|
||||
|
||||
[*.{ts,js,tsx,jsx,lua,sh}]
|
||||
charset = utf-8
|
||||
indent_style = space
|
||||
indent_size = 2
|
||||
|
||||
[*.{go,bb}]
|
||||
indent_style = tab
|
||||
|
||||
[Makefile]
|
||||
indent_style = tab
|
||||
|
||||
[*.{yml,json}]
|
||||
indent_style = space
|
||||
indent_size = 2
|
||||
|
||||
[meson.{build,options}]
|
||||
indent_style = space
|
||||
indent_size = 2
|
||||
|
||||
26
.gitignore
vendored
Normal file
26
.gitignore
vendored
Normal file
@@ -0,0 +1,26 @@
|
||||
# CMake/Meson
|
||||
build*
|
||||
cmake-build-*
|
||||
|
||||
# IntelliJ
|
||||
.idea
|
||||
|
||||
# LSPs
|
||||
.ccls
|
||||
.ccls-cache
|
||||
.cache
|
||||
compile_commands.json
|
||||
|
||||
# JetBrains Fleet
|
||||
.fleet
|
||||
|
||||
# macOS
|
||||
.DS_Store
|
||||
.AppleDouble
|
||||
.LSOverride
|
||||
|
||||
# Visual Studio Code
|
||||
.vscode
|
||||
|
||||
# ImGui
|
||||
imgui.ini
|
||||
23
README.md
Normal file
23
README.md
Normal file
@@ -0,0 +1,23 @@
|
||||
# Hello Linker Script!
|
||||
|
||||
Trying out linker script.
|
||||
|
||||
## Requirement
|
||||
|
||||
- [meson](https://mesonbuild.com/)
|
||||
|
||||
## Development
|
||||
|
||||
```sh
|
||||
meson setup build
|
||||
```
|
||||
|
||||
```sh
|
||||
ninja -C build
|
||||
```
|
||||
|
||||
## Resources
|
||||
|
||||
- [Mastering the GNU linker script](https://allthingsembedded.com/post/2020-04-11-mastering-the-gnu-linker-script/)
|
||||
- [Linker Scripts](https://home.cs.colorado.edu/~main/cs1300/doc/gnu/ld_3.html)
|
||||
|
||||
11
haxcc.c
Normal file
11
haxcc.c
Normal file
@@ -0,0 +1,11 @@
|
||||
#include "haxcc.h"
|
||||
|
||||
extern mod haxmods_begin[];
|
||||
extern mod haxmods_end[];
|
||||
|
||||
mod* mod_begin(void) {
|
||||
return haxmods_begin;
|
||||
}
|
||||
mod* mod_end(void) {
|
||||
return haxmods_end;
|
||||
}
|
||||
18
haxcc.h
Normal file
18
haxcc.h
Normal file
@@ -0,0 +1,18 @@
|
||||
#ifndef HAXCC_H
|
||||
#define HAXCC_H
|
||||
|
||||
#define HAX_MOD(name) \
|
||||
static const struct mod name __attribute__((used, section("__haxmod")))
|
||||
|
||||
typedef int (*init_fn)(void);
|
||||
typedef int (*done_fn)(void);
|
||||
|
||||
typedef struct mod {
|
||||
init_fn init;
|
||||
done_fn done;
|
||||
} mod;
|
||||
|
||||
mod* mod_begin(void);
|
||||
mod* mod_end(void);
|
||||
|
||||
#endif // HAXCC_H
|
||||
9
haxcc.ld
Normal file
9
haxcc.ld
Normal file
@@ -0,0 +1,9 @@
|
||||
SECTIONS
|
||||
{
|
||||
haxmods (READONLY) : ALIGN(4) {
|
||||
PROVIDE(haxmods_begin = .);
|
||||
KEEP(*(__haxmod))
|
||||
PROVIDE(haxmods_end = .);
|
||||
}
|
||||
}
|
||||
INSERT AFTER .text
|
||||
24
hello.cpp
Normal file
24
hello.cpp
Normal file
@@ -0,0 +1,24 @@
|
||||
#include <cstdio>
|
||||
|
||||
extern "C" {
|
||||
#include "haxcc.h"
|
||||
}
|
||||
|
||||
auto main() -> int {
|
||||
std::printf("Hello, World!\n");
|
||||
std::printf("begin: %p, end: %p, len: %lu\n", (void*)mod_begin(), (void*)mod_end(), ((long)mod_end() - (long)mod_begin()) / sizeof(mod));
|
||||
std::printf("\n");
|
||||
|
||||
for (auto* it = mod_begin(); it != mod_end(); ++it) {
|
||||
it->init();
|
||||
}
|
||||
|
||||
std::printf("\n");
|
||||
std::printf("we're have init everything\n");
|
||||
std::printf("\n");
|
||||
|
||||
for (auto* it = mod_begin(); it != mod_end(); ++it) {
|
||||
it->done();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
23
meson.build
Normal file
23
meson.build
Normal file
@@ -0,0 +1,23 @@
|
||||
project(
|
||||
'haxcc',
|
||||
['c', 'cpp'],
|
||||
version : '0.1',
|
||||
default_options : [
|
||||
'warning_level=3',
|
||||
'c_std=c2x',
|
||||
'cpp_std=c++23'
|
||||
])
|
||||
|
||||
ldscript = 'haxcc.ld'
|
||||
|
||||
executable(
|
||||
'haxcc',
|
||||
[
|
||||
'hello.cpp',
|
||||
'haxcc.c',
|
||||
'mods/mod_a.c',
|
||||
'mods/mod_b.c',
|
||||
],
|
||||
link_args: ['-Wl,-T', meson.source_root() / ldscript, '-no-pie' ],
|
||||
install : false
|
||||
)
|
||||
18
mods/mod_a.c
Normal file
18
mods/mod_a.c
Normal file
@@ -0,0 +1,18 @@
|
||||
#include "haxcc.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int mod_a_init(void) {
|
||||
printf("module a init\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int mod_a_done(void) {
|
||||
printf("module a done\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
HAX_MOD(mod_a) = {
|
||||
.init = mod_a_init,
|
||||
.done = mod_a_done
|
||||
};
|
||||
18
mods/mod_b.c
Normal file
18
mods/mod_b.c
Normal file
@@ -0,0 +1,18 @@
|
||||
#include "haxcc.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
static int mod_b_init(void) {
|
||||
printf("top kek MODB!\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int mod_b_done(void) {
|
||||
printf("MODB do be done!\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
HAX_MOD(mod_b) = {
|
||||
.init = mod_b_init,
|
||||
.done = mod_b_done
|
||||
};
|
||||
Reference in New Issue
Block a user