Android ELF file compilation symbol hiding

The compilation system is a very powerful system with many compilation parameters, but most people have only heard a few, and hope to have time to look at the relevant data and study clearly.

Today, I will introduce how to hide symbols when compiling so under the Android NDK. By default, gcc exports all symbols. For security reasons, you can hide symbols, which can cause great trouble for cracking. In addition, you can Effectively reducing the size of the so file, another advantage is that it can speed up the running speed of the program.

There are basically several ways to hide symbols: one is to add the visibility attribute (__attribute __ ((visibility ("hidden")) in the function definition; one is to add -fvisibility = hidden compilation option in Android.mk; --version-script option, and compile version script script.

Not to mention the first two, after adding and compiling with readelf, you can find that there is no function you defined in the symbol table.


Let ’s talk about the third option, version-script. The version script is a script file written by the user that can be directly compiled into the ld link script. It looks a bit confusing. To put it bluntly, the linker will write you when linking The version script script is added to the link script, and the linker will execute the commands in the version script to implement some functions.

The main function of the linker is to realize the link from the symbol to the address. In this process, the linker will define a tree structure, in which each node (version node) is composed of the symbol name and interdependence defined in the version script. The version script can specify that a certain symbol is bound to a certain version node (version node), so that some symbols can be set to the local area, then these symbols can be hidden from the outside of the so.

The format of the version script is as follows:

VER1 {
	 global:
		 foo1;
	 local:
		 old*; 
		 original*; 
		 new*; 
};
Among them, VER1 is something like a function in the script, which can also be called a label. You can define multiple labels similar to VER2, VER3, and so on. They are used to distinguish different symbols in the compiled so, that is, certain symbols are bound to this label on.

Global defines symbols that are visible to the outside world.

local is a symbol that is only visible inside.

* And? Are wildcards to match multiple and one character respectively.

In this way, you can define the visibility of the symbols through the version script script. You do n’t have a thorough understanding. Let ’s write so much first.


Reference link:

1.http://man7.org/conf/lca2006/shared_libraries/slide18c.html

2.http://ftp.gnu.org/old-gnu/Manuals/ld-2.9.1/html_node/ld_25.html

3.http://www.gnu.org/software/gnulib/manual/html_node/LD-Version-Scripts.html

Published 60 original articles · Like 44 · Visits 340,000+

Guess you like

Origin blog.csdn.net/beyond702/article/details/56281068