Skip to main content

4 posts tagged with "c"

View All Tags

Date created: 2024-10-01

Description

To build binaries for the ARM architecture, and run them on an x86 based processor inside of linux, we need to set up an environment involving some specialised tools.

Prerequisites

  • qemu (qemu-user)
  • gcc-arm-linux-gnueabihf

Setup

  • Install the required tools
$ sudo apt install build-essential gcc-arm-linux-gnueabihf qemu-user
  • Assemble the target code using the cross-compiler
$ arm-linux-gnueabihf-gcc -o <binary-name> <source-code.c> -static
  • Run the programme
$ qemu-arm <./binary-name>

Date created: 2024-10-06

Description

Inline functions are a [[c-compiler-suggestion|suggestion for the compiler]] to place the function code inline to where it's been executed. This is done for optimisation reasons.

Go wild here

Consider a simple C function like

inline uint8_t vga_entry_colour(enum vga_colour fg, enum vga_colour bg) {
return fg | bg << 4;
}

All it does is a simple [[bitwise-operations|bitwise operation]]. If this function was declared as a simple function (not inline), every time we call it, the CPU would have to do all the regular things, jump to an address, prepare the stack, execute, pop the stack, return.

If the function is inline though, this gives the suggestion to the compiler to place the function code inline wherever it's being called, meaning the CPU doesn't have to do all the regular function-call things. This has the potential to improve performance if the function is being called many many times over.

Tags:

Date created: 2024-10-06

Description

Static functions restrict access to themselves only to the file they have been declared. This also means, we can reuse the function name in other files.

Go wild here

static int fun(void) {
printf("I am a static function");
}
Tags:

Date created: 2024-10-01

Description

Normally, we would use gdb to debug binaries on linux, but we need a special setup to debug binaries targetting the ARM architecture while working on a x86 based computer.

Prerequisites

  • gdb-multiarch

Setup

  • Install gdb-multiarch
$ sudo apt install gdb-multiarch
  • Build as shown in [[build-binary-for-arm]].

  • Run the programme in gdb server using [[qemu]]

$ qemu-arm <./binary-name> -g 4242
  • Debug the programme
$ gdb-multiarch 
(gdb) file <./binary-name>
(gdb) target remote localhost:4242