Skip to content

Instantly share code, notes, and snippets.

@RomanKharin
Created March 11, 2015 13:59
Show Gist options
  • Save RomanKharin/3b5b5d40d4270c198e81 to your computer and use it in GitHub Desktop.
Save RomanKharin/3b5b5d40d4270c198e81 to your computer and use it in GitHub Desktop.
Show auxilary vectors (flat assembler)
; Show Auxiliary Vectors (AT_*)
; compile:
; > fasm showauvec.fasm showauvec
; >> flat assembler version 1.71.38 (16384 kilobytes memory)
; >> 3 passes, 1179 bytes.
; romiq.kh@gmail.com
; see also http://articles.manugarg.com/aboutelfauxiliaryvectors
; https://github.com/torvalds/linux/blob/v3.19/include/uapi/linux/auxvec.h
format ELF executable 3
entry start
;================== code =====================
segment readable executable
;=============================================
start:
mov eax, 4 ; System call 'write'
mov ebx, 1 ; 'stdout'
mov ecx, msg1 ; Address of message
mov edx, msg1.size
.end:
int 0x80
mov ebp, esp
call show_at
mov eax, 1 ; System call 'exit'
xor ebx, ebx ;
int 0x80
show_at:
.argv:
add ebp, 4
mov ecx, [ebp]
test ecx, ecx
jz .env
jmp .argv
.env:
add ebp, 4
mov ecx, [ebp]
test ecx, ecx
jz .at
jmp .env
.at:
add ebp, 4
mov eax, [ebp] ; AT_ code
call print_at
call print_eq
add ebp, 4
mov ebx, [ebp] ; AT_ value
push eax
mov eax, ebx
mov edi, msghex.val
mov ecx, 8
call eax2hex
call print_hex
pop eax
call print_ln
test eax, eax
jnz .at
.at_end:
ret
print_hex:
push eax ebx ecx edx
mov eax, 4 ; System call 'write'
mov ebx, 1 ; 'stdout'
mov ecx, msghex ; Address of message
mov edx, msghex.size ; Length of message
int 0x80 ; All system calls are done via this interrupt
pop edx ecx ebx eax
ret
print_eq:
push eax ebx ecx edx
mov eax, 4
mov ebx, 1
mov ecx, msgeq
mov edx, msgeq.size
int 0x80
pop edx ecx ebx eax
ret
print_ln:
push eax ebx ecx edx
mov eax, 4
mov ebx, 1
mov ecx, msgln
mov edx, 1
int 0x80
pop edx ecx ebx eax
ret
print_at:
push eax ebx
mov esi, at_tbl
.atloop:
mov ecx, [esi]
add esi, 4
mov edx, [esi]
add esi, 4
cmp ecx, eax
jz .atfnd
cmp ecx, 0
jz .atno
jmp .atloop
.atfnd:
mov ecx, edx
call strlen
mov eax, 4
mov ebx, 1
int 0x80
jmp .atend
.atno:
mov edi, msghex.val
mov ecx, 8
call eax2hex
mov eax, 4 ; System call 'write'
mov ebx, 1 ; 'stdout'
mov ecx, msgnoval ; Address of message
mov edx, msghex.size+2+5 ; Length of message
int 0x80 ; All system calls are done via this interrupt
.atend:
pop ebx eax
ret
; Takes eax and turns it into a hex-string in [edi] (16 or 32-bit)
eax2hex: ;di to di+cx
push eax ebx edx
mov ebx,16
.l:
xor edx,edx
div ebx
add edx,"0"
cmp edx,"9"
jbe @f
add edx,"A"-"9"-1
@@:
mov [edi+ecx-1],dl
sub ecx,1
jne .l
pop edx ebx eax
ret
strlen:
; return string [ecx] length in edx
push eax
xor edx, edx
mov edi, ecx
.l:
mov al, [edi + edx]
test al, al
jz .end
inc edx
jmp .l
.end:
pop eax
ret
;================== data =====================
segment readable writeable
;=============================================
msg1_ db 'Message #1', 0xA
msg1_.size = $-msg1
macro defstr id, str
{
id db str
.size = $-id
}
defstr msg1, <'Auxiliary Vectors:', 0xA>
msgnoval db ' '
msghex db '0x'
msghex.val db 0,0,0,0,0,0,0,0
msghex.size = $-msghex
db ' '
msgln db 0xA
defstr msgno, <'Unknown '>
defstr msgeq, <' = '>
macro defvec vector, value
{
vector db ' '#`vector
STLEN_#vector equ $-vector
if ($-vector < 18)
db 17-($-vector) dup(' ')
end if
db 0x0
}
macro devidx vector, value
{
dd value
dd vector
}
macro deftab mcr
{
mcr AT_IGNORE, 1 ; Entry should be ignored
mcr AT_EXECFD, 2 ; File descriptor of program
mcr AT_PHDR, 3 ; Program headers for program
mcr AT_PHENT, 4 ; Size of program header entry
mcr AT_PHNUM, 5 ; Number of program headers
mcr AT_PAGESZ, 6 ; System page size
mcr AT_BASE, 7 ; Base address of interpreter
mcr AT_FLAGS, 8 ; Flags
mcr AT_ENTRY, 9 ; Entry point of program
mcr AT_NOTELF, 10 ; Program is not ELF
mcr AT_UID, 11 ; Real uid
mcr AT_EUID, 12 ; Effective uid
mcr AT_GID, 13 ; Real gid
mcr AT_EGID, 14 ; Effective gid
mcr AT_PLATFORM, 15 ; string identifying CPU for optimizations
mcr AT_HWCAP, 16 ; arch dependent hints at CPU capabilities
mcr AT_CLKTCK, 17 ; Frequency of times()
; AT_* values 18 through 22 are reserved
mcr AT_SECURE, 23 ; secure mode boolean
mcr AT_BASE_PLATFORM, 24 ; string identifying real platform, may
; differ from AT_PLATFORM.
mcr AT_RANDOM, 25 ; address of 16 random bytes
mcr AT_HWCAP2, 26 ; extension of AT_HWCAP
mcr AT_EXECFN, 31 ; filename of program
mcr AT_SYSINFO, 32
mcr AT_SYSINFO_EHDR, 33
mcr AT_NULL, 0 ; End of vector
}
; define string table
deftab defvec
at_tbl:
deftab devidx
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment