網頁

2011年6月14日 星期二

LD_DEBUG and LD_LIBRARY_PATH

It is useful to find out which library are loaded when running the program.
LD_DEBUG = help ./a.out
Vaild option for LD_DEBUG
libsdisplay library search paths
relocdisplay relocation processing
filesdisplay progress for input file
symbolsdisplay symbol table processing
bindingsdisplay information about symbol binding
versionsdisplay version dependencies
allall previous options combined
statisticsdisplay relocation statistic
unuseddetermined unused DSOs
helpdisplay this help message and exit
LD_DEBUG=libs ./a.out
17315:    find library=libc.so.6 [0]; searching
17315:     search cache=/etc/ld.so.cache
17315:      trying file=/lib/tls/i686/cmov/libc.so.6
17315:
17315:
17315:    calling init: /lib/tls/i686/cmov/libc.so.6
17315:
17315:
17315:    initialize program: ./a.out
17315:
17315:
17315:    transferring control: ./a.out
17315:
17315:
17315:    calling fini: ./a.out [0]
17315:
17315:
17315:    calling fini: /lib/tls/i686/cmov/libc.so.6 [0]
17315:
From above result, it will find the file "ld.so.conf" to know where the standard library is located.
In this case, the library I only use is glibc. So it is a simple code.
But it is handy tool if your program is very complex and you want to know which library is linked.
gcc --share b.c -o libb.so
gcc -o a{,.c} -lb -L.
Then you should export libary PATH "LD_LIBRARY_PATH"
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH;./
The program will be executed successfully.
You can try LD_DEBUG again and the loader will search additional path "./" you assigned  and load the specific library to memory.

2011年6月7日 星期二

Overlay attribute in LDScript

For each section within the OVERLAY, the linker automatically defines two symbols. The symbol __load_start_secname is defined as the starting load address of the section. The symbol __load_stop_secname is defined as the final load address of the section. 
OVERLAY 0x1000 : AT (0x4000)
{
   .text0 { o1/*.o(.text) }
   .text1 { o2/*.o(.text) }
}
This will define both `.text0' and `.text1' to start at address 0x1000. `.text0' will be loaded at address 0x4000, and `.text1' will be loaded immediately after `.text0'. The following symbols will be defined: __load_start_text0__load_stop_text0,__load_start_text1__load_stop_text1.
C code to copy overlay .text1 into the overlay area might look like the following.
extern char __load_start_text1, __load_stop_text1;
memcpy ((char *) 0x1000, &__load_start_text1,
          &__load_stop_text1 - &__load_start_text1);
Note that the OVERLAY command is just syntactic sugar, since everything it does can be done using the more basic commands. The above example could have been written identically as follows.
.text0 0x1000 : AT (0x4000) { o1/*.o(.text) }
__load_start_text0 = LOADADDR (.text0);
__load_stop_text0 = LOADADDR (.text0) + SIZEOF (.text0);
.text1 0x1000 : AT (0x4000 + SIZEOF (.text0)) { o2/*.o(.text) }
__load_start_text1 = LOADADDR (.text1);
__load_stop_text1 = LOADADDR (.text1) + SIZEOF (.text1);
. = 0x1000 + MAX (SIZEOF (.text0), SIZEOF (.text1));
Reference from The GNU linker.

2011年6月4日 星期六

Weak function

From gnu gcc document:
The weak attribute causes the declaration to be emitted as a weak symbol rather than a global. This is primarily useful in defining library functions which can be overridden in user code, though it can also be used with non-function declarations.

libweak.c
#include <stdio.h>

void weakfunc() __attribute__((weak));

void weakfunc()
{
    printf ("default weak function\n");

}
main.c
#include <stdio.h>
void weakfunc()
{
    printf ("weak function from main\n");    
}

int main ()
{
    weakfunc();
    return 0;
}
Compile the library and main function.
#gcc -shared -o libweak.so weakfunc.c
#gcc  -o weakfunc{,.c} -lweak -L.
#LD_LIBRARY_PATH=. ./main
weak function from main 
From above, it is very clear that the weak function of the library is replaced.
#objdump -t libweak.so |grep weakfunc
0000046c  w    F .text 00000014   weakfunc
In the library, this function has weak attribute.
#objdump -t main |grep weakfunc
080484e4  g    F .text 00000014   weakfunc
However, this function is presented as  global attribute.

2011年6月2日 星期四

Andriod 專有名詞整理

最近研究了Andriod,整理了一些專有名詞如
IPL (InitialProgramLoader):
Initialize the hardware setting, such as memory, cpu clock , flash, about the development board.
It is similar with the bootloader in the embedded system, like u-boot.

SPL (Second Program Loader):
The main goal is to load system OS to memory.
In addition, some features is provided as the below list.
(1) check hardware, like LCD, led.
(2) search the active partion and load it to memory.
(3) connection with PC and recieve commands from PC host.

HBoot:
This is  HTC’s SPL
fastboot:
Fastboot is protocol used to update the flash filesystem in Android devices from a host over USB.
The command "fastboot.exe" you can run on your host after fastboot has been started on a device connected via USB.
e.g.
$ fastboot erase boot

Andriod Recovery:
The recovey of  the mainstream andriod can provide some options to recovery your systems or factory-to-default, even
though it can load official ROM in the zip file of SD card and recovey the system.
ClockworkMod recovery is one of the widely used custom andriod recovery and it provide more options to recovery unofficial or official
ROM from the zip file of SD card.