Posts

Showing posts from March, 2010

Shared libraries and ELF

I just finished reading How to write Shared Libraries by Ulrich Drepper, a very nice guide to understanding the details of dynamic linking and use of ELF format with Linux. ELF structure like program header and segments Clear explanation of GOT and PLT with concrete examples Types of relocations: relative, symbol relocation and evils of TEXTREL Tracing dynamic linking w/ LD_DEBUG concept of gnu-hash style and backwards compatibility with -hash-style=both attributes constuctor, destructor common variables and -fno-common controlling exported symbols with static / -fvisibility / attribute / #pragma visibility / export maps symbol versioning: implementing backwards compatibility with a single dso: export maps and symver foo@VERS1 for past versions / symver foo@@VERS2 for the latest dangers of depending on unfined symbols and -Wl,-z,defs deprecated rpath vs runpath relative paths to dso:s using dynamic string tokens like $ORIGIN --as-needed to prune the list of DSO in DT_NEEDED

gnu binutils

I decided to read through the man pages of binutils, to see if there's something interesting I've never come across. The only things that were not previously very familiar were the c++filt and addr2line tools. Anyway, here are the notes I made while reading about each of the tools in the binutils package. ar and ranlib manage archives: static libraries of compiled object files. Most often these are used indirectly through libtool and automake. nm Lists symbols from object files. This is useful when troubleshooting linking problems. With nm you can check which data and functions are actually found in each intermediate object or library. objcopy Copies and converts object files. Typical use of objcopy is to convert and ELF file into a binary file. This is effectively pre-computing the work of an elf loader for targets where elf loader is not relevant at run time, like ROM code. Objcopy can also work the other way. Objcopy can wrap a binary file in an elf object, so that binary d

Dangers of C library

I just read through Ulrich Drepper's Defensive Programming article. He highlights dangers of C library, provides safer alternatives for commonly used idioms and introduces a number of tools for pinpointing problems in code. These are my rather unstructured notes of things most interesting to me. Syntax for forward declarations within paramer list and array size annotation for future checks int addvec(int n; int arr[n], int n) tools for formatting an arbitrarily long string aprintf() which allocates the buffer, for constant format string only FILE *open_memstream() , for an ostringstream -like in-memory file fast string handling within stack strdupa() and alloca() parsing strings of arbitrary size %as format string with buffer allocated by scanf parsing simple delimited files with arbitrary length of lines ssize_t getdelim() ssize_t getline() practical examples of using file descriptor variants of regular functions for avoiding race conditions eg. chown -> fchown O_NOFO

Booting Linux over JTAG

Note to self. A working gdb script to boot Linux kernel on an atmel board over JTAG. Unfortunately this is not completely automatic. u-boot needs to be interrupted for loading the uImage through gdb. U-boot can then be resumed and kernel booted with bootm 0x22000000 . Also, either the emulator needs to be configured to ignore data aborts or kernel futex support to be disabled. target remote emulator:2001 monitor reset halt define bootstrap file /path/to/at91sam9260ek/at91sam9260ek-nandflashboot-2.11-r1.elf load j *0x200000 #bootstrap entry point in sram end define u-boot file /path/to/u-boot/u-boot load j *0x23f00000 # u-boot entry point in sdram end define linux restore /path/to/arch/arm/boot/uImage binary 0x22000000 file /path/to/vmlinux end

Optimizing with gcc

My notes while reading Optimizing applications with gcc & glibc by Ulrich Drepper. __extension__ to mark intentional use of GNU extension. poor mans 'lambda' (({ })) predicate to determine if parameter value is constant at runtime: __builtin_constant_p() mark pure functions with __attribute__((__const__)) mark functions which never return with __attribute__((__noreturn__)) __stdcall__ attribute on x86 makes callee correct the stack at ret, in Unix convention the caller corrects the stack mem p cpy returns a pointer just after the last copied byte. calloc knows if memory is already full of 0's -> faster than malloc & memset int_least16_t, int_fast16_t when actual variable size is uninteresting, speed is replace stlen and + with strchr(s,'\0') or rawmemchr() gcc computed gotos and jump tables: && for the address of a label -pg and -profile : gprof and sprof

Eclipse CDT

I'll have to try out the Eclipse CDT sometime - integration of native development tools such as the GNU Autotools: Linux Tools - a plugin to explore remote system files through Eclipse : eclipse-rse

64bit PC's and memory

Just to refresh my knowledge on the PC platforms, I read the excellent Wikipedia article on the 64-bit x86 . PC processors and 64-bitness The current 64-bit instruction set is a backwards compatible extension to the Intel instruction set. It is implemented in all typical processors from all common vendors. It has multiple confusing names, all meaning the same: Intel 64, amd64, EM64T, x86_64, x86-64, IA-32e. Intel Itanium (IA-64) is a completely different thing, and has nothing to do with current 64bit systems. Not all of the 64 bits are in use today. Some of them are cleverly saved for the future by dividing the virtual address space into two halves. By having the unused gap in the middle as a forbidden zone, the address bits reserved for future use are safe from short term abuse by implementations. Maximum physical memory in a PC Having more than 4GB of physical memory is not an issue for 64-bit systems. Even a single process can use more than 4GB. Using more than 4GB of physical memo

Linux Kernel in a nutshell

Although I've been working with Linux kernel for ages, I decided to browse through Linux Kernel in a nutshell , an excellent book on the practical things regarding working with the kernel. There certainly were things that I was not aware of. Here are my notes on these things. Using a read-only source tree with O=/path/to/build/output Some basic static analysis with sparse with C=1 or C=2. checkstack namespacecheck Localversion-prefixed files in object and source trees. Finding all kernel modules required by the running system, using modaliases in the /sys tree and modprobe with --show-depends. Or get-driver.sh helper script. Kernel argument max_addr to force a upper bound on physical addresses used. Probably a good for validating a software image for a low cost board version. I've used mem=XX for this before, but I believe max_addr could yield even more realistic results. The book included nice "demos" for tools like quilt for managing a set of patches agai