From 07304d7e1135009cb7073a9bcc295168ba9675b3 Mon Sep 17 00:00:00 2001 From: FreeArtMan Date: Wed, 24 Aug 2022 16:33:43 +0100 Subject: Added embeding notes --- md/notes/undefined_c/titles.md | 196 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 190 insertions(+), 6 deletions(-) (limited to 'md/notes') diff --git a/md/notes/undefined_c/titles.md b/md/notes/undefined_c/titles.md index 4e7361e..18fcdec 100644 --- a/md/notes/undefined_c/titles.md +++ b/md/notes/undefined_c/titles.md @@ -1,4 +1,4 @@ -title:Undefine C +title:Undefined C keywords:c,linux,asm # Undefined C @@ -1426,30 +1426,209 @@ undefined_b.c:5:14: runtime error: division by zero fish: Job 1, './undefined_b' terminated by signal SIGFPE (Floating point exception) ``` - + ### Write plugins - - ### Preload library ## Embedding C +Most of the programming languages support embeding C. As C language have where simple +functiong naming when its mangled to object format it makes it easy target when +linking with other languages. Most of other languages have incompatible naming for +functions when compiled to binary. + ### Embed in C++ + +__lib.h__ +```c +#include +#include + +int fun_secret_1(); +``` + +__lib.c__ +```c +#include "lib.h" + +int fun_secret_1() { + printf("Hello from C\n"); + return -1; +} +``` + +First thing to notice is when file is compiled with C++ is that the name of the function are in different format +then when its compiled with C. +``` +$ g++ -c lib.c +$ readelf -s lib.o +Symbol table '.symtab' contains 6 entries: + Num: Value Size Type Bind Vis Ndx Name + 0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND + 1: 0000000000000000 0 FILE LOCAL DEFAULT ABS lib.c + 2: 0000000000000000 0 SECTION LOCAL DEFAULT 1 .text + 3: 0000000000000000 0 SECTION LOCAL DEFAULT 5 .rodata + 4: 0000000000000000 26 FUNC GLOBAL DEFAULT 1 _Z12fun_secret_1v + 5: 0000000000000000 0 NOTYPE GLOBAL DEFAULT UND puts + +``` + +Lets tell C++ that file is C language by adding **extern "c"** + +__lib.h__ +```c +#include +#include + +extern "C" { + +int fun_secret_1(); + +} +``` + +__lib.c__ +```c +#include "lib.h" + +extern "C" { + +int fun_secret_1() { + printf("Hello from C\n"); + return -1; +} + +} +``` + +Now compiled object file have C function names. + +``` +$ g++ lib.c -c +$ readelf -s lib.o + +Symbol table '.symtab' contains 6 entries: + Num: Value Size Type Bind Vis Ndx Name + 0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND + 1: 0000000000000000 0 FILE LOCAL DEFAULT ABS lib.c + 2: 0000000000000000 0 SECTION LOCAL DEFAULT 1 .text + 3: 0000000000000000 0 SECTION LOCAL DEFAULT 5 .rodata + 4: 0000000000000000 26 FUNC GLOBAL DEFAULT 1 fun_secret_1 + 5: 0000000000000000 0 NOTYPE GLOBAL DEFAULT UND puts + +``` + +__cppembed.cpp___ +```cpp +#include "lib.h" + +int main() { + fun_secret_1(); +} +``` + +Doing oposite way running C++ from C +[/writeup/wraping_c_plus_plus_exceptions_templates_and_classes_in_c.md](/writeup/wraping_c_plus_plus_exceptions_templates_and_classes_in_c.md) + ### Embed in Go + +__lib.h__ +```c +#include +#include + +int fun_secret_1(); +``` +__lib.c__ +```c +#include "lib.h" + +int fun_secret_1() { + printf("Hello from C\n"); + return -1; +} +``` + +__main.go__ +```go +package main +// #cgo CFLAGS: -g -Wall +// #include +// #include "lib.h" +import "C" +import ( + "fmt" + +) +func main() { + fmt.Println("Start program") + C.fun_secret_1() + fmt.Println("End program") +} +``` + +``` +go build +``` + +[https://karthikkaranth.me/blog/calling-c-code-from-go/](https://karthikkaranth.me/blog/calling-c-code-from-go/) + + ### Embed in Swift -### Embed in JS + +[/writeup/linux_hello_world_in_swift.md](/writeup/linux_hello_world_in_swift.md) + +### Embed in Rust + +__lib.c__ +```c +#include +#include + +int fun_secret_1() { + printf("Hello from C\n"); + return -1; +} +``` + +```rust +extern "C" { + fn fun_secret_1(); +} + +//rustc main.rs -o hello +fn main() { + println!("Start program"); + unsafe {fun_secret_1()} + println!("End program"); +} +``` + +Compile with + +``` +gcc -c lib.c +gcc -shared lib.o -o liblib.so +rustc main.rs -l lib -L . -o hello -C link-arg="-Wl,-rpath=./" +``` + +[https://dev.to/xphoniex/how-to-call-c-code-from-rust-56do](https://dev.to/xphoniex/how-to-call-c-code-from-rust-56do) ### Lua in C + +[/writeup/embedding_lua_in_c.md](/writeup/embedding_lua_in_c.md) + ### Python in C + ## Multiplatform ### Cross compile @@ -1459,6 +1638,11 @@ fish: Job 1, './undefined_b' terminated by signal SIGFPE (Floating point excepti ### AVR8 ### Emscripten +[/writeup/web_assembly_sdl_example.md](/writeup/web_assembly_sdl_example.md) + +#### Embed in JS + + ## Graphics -- cgit v1.2.3