Skip to content

Latest commit

 

History

History
185 lines (128 loc) · 5.63 KB

configuration.asc

File metadata and controls

185 lines (128 loc) · 5.63 KB

Configuration

Before the include of minilib.h, the minilib has to be configured.

The proposed way is to use the supplied script minimake,
which translates config options to define flags,
and pipes the minilib header to gcc.

The config file is a bash script and can be a separate file, or within one of the source files.

Since it is a bash script, it is possible to e.g.
source other files from within the configuration.

A simple example is given below:

#if 0  // has to be the first line
# mini_start is the start routine.
# Most possibly needed
mini_start

# define and compile puts
mini_puts

INCLUDESRC
# this is a comment.
# INCLUDESRC has to be defined for one sourcefile.
# without it only the defines, and no implementations are parsed.

# shrink the binary with tools/shrinkelf
SHRINKELF

# use the linker script onlytext
# meaning, only text and stack sections are loaded
LDSCRIPT onlytext

# return, and the #endif statement are the last lines of the config
return
#endif

int main(int argc, char *argv[] ){
    puts("Hello, world!");
		return(0);
}

Only functions defined are going to be built.

There are several examples given within the folder examples,
the files within the folder test are there for regression tests,
but might also be helpful.

To compile a c source file with embedded configuration (as in the example above),
type 'minimake --config example.c -o example example.c'.
(Shortcut 'minimake -mlc example.c')

(minimake needs to be in the standard search path)
If you’d like to "install" the minimake, simply copy the script minimake into /usr/local/bin,
or wherever your executables live and can be found.

The minilib itself, as well as the supllied linker scripts are embedded into minimake,
and extracted and piped to gcc on demand.

(There are compatibility headers provided, e.g. stdio.h;
these are however not stable and will most probably not work.
I’m going to update them, as soon other and more important things are finished.)

In addition to the switches, which are built of the function’s name and the prefix 'mini_';
e.g. 'mini_puts', these switches are defined for the configuration of minilib:

mini_start

Built the start routine, which is needed for the execution of the binary.

mini_buf 512

the buf size in Bytes (here 512Bytes), which is used by minilib for input/output functions, the globals and malloc/free. Uncomment this, if you do not need globals or functions, depending on the buffer.

globals_on_stack

put the minibuf and all globals onto the stack (this enables to omit all sections besides text and stack of the binary, when nowhere else globals (ans static variables) are used, for aggressive size optimization) The address of the globals is going to be stored within a fixed cpu register. (r15 at amd64) The globals are stored at the end of the stack, behind the first return addresse. (They are setup within startup.c; however, startup calls main, but never returns itself, instead the function exits via syscall. So it is not possible to overwrite the return addresse with a Bufferoverflow. In case you have to work with unsafe userinput, please have a look into map_protected)

mini_errno

generate the code for the errno variable.
This generates some overhead
(a few bytes per every syscall, and every function which sets errno; globals are generated as well)
Without the switch, syscalls do return the negative errno value on failure.

mini_syscalls

define the syscall wrappers, starting with 'sys_'

HEADERGUARDS

define the headerguards, which hopefully prevent the inclusion of the standard library headers.

OPTFLAG -Os

optimization Flag. Os,O1,O2 should be save. O3 causes sometimes troubles

e.g. OPTFLAG '-g -O0'
(compile with debug info)
OPTIMIZE

add some speed optimizations, mainly vectorizations for 64bit.
This comes naturally with a memory penalty,
yet, the raise in memory usage is neglectible. (a few hundred bytes)

STRIPFLAG

stripflag ( defaults to -s)
Leaving 'STRIPFLAG' alone removes the flag parameter from gcc’s commandline.

SOURCES file1 file2 …​

(optional)
define other files as sources, to be compiled into the binary.

BINARY name

(optional)
Set the name of the resulting binary.+ (supplied to gcc as -o name)

INCLUDESRC

Build minilib with source.
Without this switch, only defines and declarations are included.
When building with several object files, most probably you should
enable this flag only for one object file.
It’s also possible to pass the switch via a define flag to gcc.
('-DINCLUDESRC')

LDSCRIPT default

The ldscript to use
Defines, which executable sections are going to be built.
Can be one of:

  • default: .text .data .rodata .bss (.stack)

  • default_execstack: .text .data .rodata .bss (.stack)
    The stack is executable, what is needed for e.g. closures+

  • textandbss: .text .bss (.stack)

  • onlytext: .text (.stack)

  • onlytext_tiny: .text (the stack is defined by the kernel, and made executable)
    Despite general meaning, an executable stack isn’t harmful on its own,and even widely used. It just is important (as always), to check user input against e.g. overflows.

SHRINKELF

Shrink the compiled binary with shrinkelf.
Aggressively strip the elf headers.

DEBUG

generate debug info (-O0 -g). Overwrites OPTFLAG, STRIPFLAG and SHRINKELF

DEFINE def value

add a #define def value "def" and "value" have to be within apostrophes, when they contain either brackets,spaces, or other operators. Multilines are possible, with a preceding \\ before the linebreak.