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

Add support for init/fini arrays to libc #1725

Merged
merged 1 commit into from
Apr 13, 2017

Conversation

akosthekiss
Copy link
Member

The arrays contain the addresses of functions annotated with
constructor or destructor attributes. The support is optional,
requires FEATURE_INIT_FINI cmake option to be set. As of now, the
option is not available in tools/build.py directly, only via
--cmake-param="-DFEATURE_INIT_FINI=ON".

JerryScript-DCO-1.0-Signed-off-by: Akos Kiss [email protected]

@akosthekiss akosthekiss added the jerry-libc (Deprecated) Related to the jerry-libc library label Apr 10, 2017
@akosthekiss
Copy link
Member Author

This patch is a proposed solution for #1722.

The constructor/destructor behavior can be tested, e.g., by adding the below two functions anywhere in the compiled sources. (For my experiments, I've copied them into jerry-main/main-unix.c.)

static void __attribute__ ((constructor))
start_jerry (void)
{
  printf ("JERRY STARTED\n");
}

static void __attribute__ ((destructor))
finish_jerry (void)
{
  printf ("JERRY FINISHED\n");
}

If configured properly, start_jerry and finish_jerry will be invoked even though they are not called anywhere explicitly. First example on ARM32.

$ ./tools/build.py --clean --cmake-param="-DFEATURE_INIT_FINI=ON" --toolchain cmake/toolchain_linux_armv7l.cmake
$ file ./build/bin/jerry
./build/bin/jerry: ELF 32-bit LSB  executable, ARM, EABI5 version 1 (SYSV), statically linked, BuildID[sha1]=5863cc6fdcdcf956d6910a266f7ce583fd20c995, stripped
$ qemu-arm-static ./build/bin/jerry ./tests/hello.js
JERRY STARTED
Hello JerryScript!
JERRY FINISHED

It also works on x86-64:

$ ./tools/build.py --clean --cmake-param="-DFEATURE_INIT_FINI=ON"
$ file ./build/bin/jerry 
./build/bin/jerry: ELF 64-bit LSB  executable, x86-64, version 1 (SYSV), statically linked, BuildID[sha1]=73dba6c37fed7aa9aea19b533e76f1d2bfcf28e2, stripped
$ ./build/bin/jerry ./tests/hello.js 
JERRY STARTED
Hello JerryScript!
JERRY FINISHED

Sanity check: if the feature is not enabled in jerry-libc, the constructors/destructors don't run.

$ ./tools/build.py --clean
$ ./build/bin/jerry ./tests/hello.js
Hello JerryScript!

Note 1: Haven't tested on x86.

Note 2: Had to modify cppcheck suppression list, as cppcheck incorrectly reported void * arithmetics. Actually, arithmetics is performed on function pointers for which the return type is void, but that should be a completely different story, IMHO.

jerry-libc/jerry-libc-init.c:62: portability(arithOperationsOnVoidPointer): '__preinit_array_end' is of type 'void *'. When using void pointers in calculations, the behaviour is undefined.

@akosthekiss
Copy link
Member Author

An additional sanity check: the enhanced jerry-libc works just like the system libc (which needs no extra feature switches, of course).

$ ./tools/build.py --clean --jerry-libc=off --jerry-libm=off --static-link=off --link-lib="-lm"
$ file ./build/bin/jerry 
./build/bin/jerry: ELF 64-bit LSB  executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=138ef44c61b06bb6fe7f5b0b36be1994b6b4d2c0, stripped
$ ./build/bin/jerry ./tests/hello.js 
JERRY STARTED
Hello JerryScript!
JERRY FINISHED

@gabrielschulhof
Copy link
Contributor

Super-awesome 🎉

Copy link
Member

@zherczeg zherczeg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

extern void _fini (void);


/** No-op default _init. */
Copy link
Member

@zherczeg zherczeg Apr 11, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function header comment is different for libc functions?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might have been sloppy. Will get it right asap.

The arrays contain the addresses of functions annotated with
constructor or destructor attributes. The support is optional,
requires FEATURE_INIT_FINI cmake option to be set. As of now, the
option is _not_ available in tools/build.py directly, only via
`--cmake-param="-DFEATURE_INIT_FINI=ON"`.

JerryScript-DCO-1.0-Signed-off-by: Akos Kiss [email protected]
@akosthekiss
Copy link
Member Author

I've fixed the function doc comments in the new file and updated the commit.

Copy link
Contributor

@LaszloLango LaszloLango left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@akosthekiss akosthekiss merged commit 4b9e458 into jerryscript-project:master Apr 13, 2017
@akosthekiss akosthekiss deleted the libc-init-fini branch April 13, 2017 15:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
jerry-libc (Deprecated) Related to the jerry-libc library
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants