|<Linux gnu gdb 2 | Main | Linux gnu nm and objdump 4 >|Site Index |Download |






MODULE 000-3








My Training Period: xx hours   






The as - A GNU Assembler



as is a portable GNU assembler (Gas).  GNU as is really a family of assemblers.  If you use (or have used) the GNU assembler on one architecture, you should find a fairly similar environment when you use it on another architecture.  Each version has much in common with the others, including object file formats, most assembler directives (often called pseudo-ops) and assembler syntax.

as is primarily intended to assemble the output of the GNU C compiler "gcc" for use by the linker "ld".  Nevertheless, we’ve tried to make as assemble correctly everything that other assemblers for the same machine would assemble.  Any exceptions are documented explicitly.  This doesn’t mean as always uses the same syntax as another assembler for the same architecture; for example, we know of several incompatible versions of 680x0 assembly language syntax.

Each time you run as, it assembles exactly one source program.   The source program is made up of one or more files.  (The standard input is also a file.)

You give as a command line that has zero or more input file names.  The input files are read (from left file name to right).  A command line argument (in any position) that has no special meaning is taken to be an input file name.

If you give as no file names it attempts to read one input file from theas standard input, which is normally your terminal.  You may have to type ctl-D to tell as there are no more programs to assemble.

Use -- if you need to explicitly name the standard input file in your command line.

If the source is empty,as produces a small, empty object file.  as may write warnings and error messages to the standard error file (usually your terminal).  This should not happen when a compiler runs as automatically.   Warnings report an assumption made so that as could keep assembling a flawed program; errors report a grave problem that stops the assembly.

If you are invoking as via the GNU C compiler, you can use the -Wa option to pass arguments through to the assembler.  The assembler arguments must be separated from each other (and the -Wa) by commas.  For example:


              gcc -c -g -O -Wa,-alh,-L file.c


This passes two options to the assembler: -alh (emit a listing to standard output with high-level and assembly source) and -L (retain local symbols in the symbol table). Usually you do not need to use this -Wa mechanism, since many compilers command-line options are automatically passed to the assembler by the compiler.  (You can call the GNU compiler driver with the -v option to see precisely what options it passes to each compilation pass, including the assembler.)

Usage: as [option...] [asmfile...]


-a[sub-option...]       turn on listings

Sub-options [default hls]:


omit false conditionals


omit debugging directives


include high-level source


include assembly


include macro expansions


omit forms processing


include symbols


list to FILE (must be last sub-option)


initially turn on alternate macro syntax


produce assembler debugging messages

--defsym SYM=VAL

define symbol SYM to given value


require executable stack for this object


don't require executable stack for this object


skip whitespace and comment preprocessing

-g --gen-debug

generate debugging information


generate STABS debugging information


generate STABS debug info with GNU extensions


generate DWARF2 debugging information


show this message and exit


show target specific options


add DIR to search list for .include directives


don't warn about signed overflow


warn when differences altered for long displacements


keep local symbols (e.g. starting with `L')


assemble in MRI compatibility mode


write dependency information in FILE (default none)




name the object-file output OBJFILE (default a.out)


fold data section into text section


print various measured statistics from execution


strip local absolute symbols


Use same format as native assembler when possible


print assembler version number and exit

-W  --no-warn

suppress warnings


don't suppress warnings


treat warnings as errors

--itbl INSTTBL

extend instruction set to include instructions matching the specifications defined in file INSTTBL






generate object file even after errors


set the width in words of the output data column of the listing


set the width in words of the continuation lines of the output data column; ignored if smaller than the width of the first line


set the max width in characters of the lines from the source file


set the maximum number of continuation lines used for the output data column of the listing




print assembler version number




do not optimize code alignment


quieting some warnings









The ld - A GNU Linker



ld is a GNU linker.  ld combines a number of objects and archive files, relocates their data and ties up symbol references. Usually the last step in compiling a program is to run ldld accepts Linker Command Language files written in a superset of AT&T’s Link Editor Command Language syntax, to provide explicit and total control over the linking process.

This version of ld uses the general purpose BFD libraries to operate on object files.  This allows ld to read, combine, and write object files in many different format for example, COFF or "a.out".  Different formats may be linked together to produce any available kind of object file.

Aside from its flexibility, the GNU linker is more helpful than other linkers in providing diagnostic information.  Many linkers abandon execution immediately upon encountering an error; whenever possible, ld continues executing, allowing you to identify other errors (or, in some cases, to get an output file in spite of the error).

The GNU linker ld is meant to cover a broad range of situations, and to be as compatible as possible with other linkers.   As a result, you have many choices to control its behavior.

Usage: ld [options] file...



Shared library control for HP/UX compatibility

-A ARCH, --architecture ARCH

Set architecture

-b TARGET, --format TARGET

Specify target for following input files

-c FILE, --mri-script FILE

Read MRI format linker script

-d, -dc, -dp

Force common symbols to be defined


  Set start address

-E, --export-dynamic

Export all dynamic symbols


Link big-endian objects


Link little-endian objects

-f SHLIB, --auxiliary SHLIB

Auxiliary filter for shared object symbol table

-F SHLIB, --filter SHLIB

Filter for shared object symbol table



-G SIZE, --gpsize SIZE

Small data size (if no size, same as --shared)


Set internal name of shared library

-I PROGRAM, --dynamic-linker PROGRAM

Set PROGRAM as the dynamic linker to use

-l LIBNAME, --library LIBNAME

Search for library LIBNAME

-L DIRECTORY, --library-path DIRECTORY

Add DIRECTORY to library search path


Set emulation

-M, --print-map

Print map file on standard output

-n, --nmagic

Do not page align data

-N, --omagic

Do not page align data, do not make text readonly


Page align data, make text readonly

-o FILE, --output FILE

Set output file name


Optimize output file


Ignored for SVR4 compatibility

-q, --emit-relocs

Generate relocations in final output

-r, -i, --relocatable

Generate relocatable output

-R FILE, --just-symbols FILE

Just link symbols (if directory, same as --rpath)

-s, --strip-all

Strip all symbols

-S, --strip-debug

Strip debugging symbols


Strip symbols in discarded sections


Do not strip symbols in discarded sections

-t, --trace

Trace file opens

-T FILE, --script FILE

Read linker script

-u SYMBOL, --undefined SYMBOL

Start with undefined reference to SYMBOL

  --unique [=SECTION]

Don't merge input [SECTION | orphan] sections


Build global constructor/destructor tables

-v, --version

Print version information


Print version and emulation information

-x, --discard-all

Discard all local symbols

-X, --discard-locals

Discard temporary local symbols (default)


Don't discard any local symbols

-y SYMBOL, --trace-symbol SYMBOL

Trace mentions of SYMBOL


Default search path for Solaris compatibility

-(, --start-group

Start a group

-), --end-group

End a group


Accept input files whose architecture cannot be determined


Reject input files whose architecture is unknown

-assert KEYWORD

Ignored for SunOS compatibility

-Bdynamic, -dy, -call_shared

Link against shared libraries

-Bstatic, -dn, -non_shared, -static

Do not link against shared libraries


Bind global references locally


Check section addresses for overlaps (default)


Do not check section addresses for overlaps


Output cross reference table


Define a symbol

--demangle [=STYLE]

Demangle symbol names [using STYLE]

--demangler DSO:FUNCTION

Set DSO and demangler function


Generate embedded relocs

-fini SYMBOL

Call SYMBOL at unload-time


Force generation of file with .exe suffix


Remove unused sections (on some targets)


Don't remove unused sections (default)


Set default hash table size close to <NUMBER>


Print option help

-init SYMBOL

Call SYMBOL at load-time


Write a map file


Do not define Common storage


Do not demangle symbol names


Use less memory and more disk I/O


Do not allow unresolved references in object files


Allow unresolved references in shared libraries


Do not allow unresolved references in shared libs


Allow multiple definitions


Disallow undefined version


Don't warn about mismatched input files


Turn off --whole-archive


Create an output file even if errors occur


Only use library directories specified on the command line

--oformat TARGET

Specify target of output file


Ignored for Linux compatibility


Reduce memory overheads, possibly taking much longer


Relax branches on certain targets

--retain-symbols-file FILE

Keep only symbols listed in FILE

-rpath PATH

Set runtime shared library search path

-rpath-link PATH

Set link time shared library search path

-shared, -Bshareable

Create a shared library

-pie, --pic-executable

Create a position independent executable


Sort common symbols by size

--spare-dynamic-tags COUNT

How many tags to reserve in .dynamic section

--split-by-file [=SIZE]

Split output sections every SIZE octets

--split-by-reloc [=COUNT]

Split output sections every COUNT relocs


Print memory usage statistics


Display target specific options

--task-link SYMBOL

Do task level linking


Use same format as native linker

--section-start SECTION=ADDRESS

Set address of named section


Set address of .bss section


Set address of .data section


Set address of .text section


How to handle unresolved symbols.

<method> is:

ignore-all, report-all, ignore-in-object-files, ignore-in-shared-libs


Output lots of information during link

--version-script FILE

Read version information script

--version-exports-section SYMBOL

Take export symbols list from .exports, using SYMBOL as the version.


Warn about duplicate common symbols


Warn if global constructors/destructors are seen


Warn if the multiple GP values are used


Warn only once per undefined symbol


Warn if start of section changes due to alignment


Report unresolved symbols as warnings


Report unresolved symbols as errors


Treat warnings as errors


Include all objects from following archives


Set DT_NEEDED tags for DT_NEEDED entries in following dynamic libs


Do not set DT_NEEDED tags for DT_NEEDED entries in following dynamic libs


Only set DT_NEEDED for following dynamic libs if used


Always set DT_NEEDED for following dynamic libs

--wrap SYMBOL

Use wrapper functions for SYMBOL



ld: supported targets: elf32-i386 a.out-i386-linux efi-app-ia32 elf32-little elf32-big srec symbolsrec tekhex binary ihex trad-core

ld: supported emulations: elf_i386 i386linux

ld: emulation specific options:



Selects group name lookup rules for DSO


Disable new dynamic tags


Enable new dynamic tags


Create .eh_frame_hdr section

-z combreloc

Merge dynamic relocs into one section and sort

-z defs

Report unresolved symbols in object files.

-z execstack

Mark executable as requiring executable stack

-z initfirst

Mark DSO to be initialized first at runtime

-z interpose

Mark object to interpose all DSOs but executable

-z loadfltr

Mark object requiring immediate process

-z muldefs

Allow multiple definitions

-z nocombreloc

Don't merge dynamic relocs into one section

-z nocopyreloc

Don't create copy relocs

-z nodefaultlib

Mark object not to use default search paths

-z nodelete

Mark DSO non-deletable at runtime

-z nodlopen

Mark DSO not available to dlopen

-z nodump

Mark DSO not available to dldump

-z noexecstack

Mark executable as not requiring executable stack

-z norelro

Don't create RELRO program header

-z now

Mark object non-lazy runtime binding

-z origin

Mark object requiring immediate $ORIGIN processing at runtime

-z relro

Create RELRO program header


Ignored for Solaris compatibility







Compile and link the assembly language usingas and ld from GNU assembly (Gas) respectively.  This is AT&T assembly language.  There are others as well such as Intel assembly, High Level Assembly (HLA), Motorola etc.  Their syntaxes are different.  For Intel, available compilers that can be used are MASM (Microsoft) and NASM.


[bodo@bakawali testbed9]$ cat testshell.s

#another example creating the shell code

.section .data

.section .text


.globl _start




        jmp ender




        xor %eax, %eax  #clean up the registers

        xor %ebx, %ebx

        xor %edx, %edx

        xor %ecx, %ecx


        mov $4, %al     #syscall write

        mov $1, %bl     #stdout is 1

        pop %ecx        #get the address of the string from the stack

        mov $0xd, %dl   #length of the string

        int $0x80


        xor %eax, %eax

        mov $1, %al     #exit the shellcode

        xor %ebx, %ebx

        int $0x80



        call starter    #put the address of the string on the stack

        .string "TESTING HELLO"


[bodo@bakawali testbed9]$ as testshell.s -o testshell.o

[bodo@bakawali testbed9]$ ld testshell.o -o testshell

[bodo@bakawali testbed9]$ ls -F -l

total 44

-rwxrwxr-x  1 bodo bodo  742 Apr 10 18:24 testshell*

-rw-rw-r--  1 bodo bodo  558 Apr 10 18:24 testshell.o

-rw-rw-r--  1 bodo bodo  661 Feb 23 15:20 testshell.s


To run, it is same as the C program.


[bodo@bakawali testbed9]$ ./testshell








Further related reading:


  1. Check the best selling C / C++, Networking, Linux and Open Source books at Amazon.com.

  2. Linux Socket programming tutorial.

  3. C, Buffer overflow and stack frame (construction and destruction).

  4. C, Compiler, Assembler and Linker.




|<Linux gnu gdb 2 | Main | Linux gnu nm and objdump 4 >|Site Index |Download |

C & Linux GNU Related Utilities: Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6