Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

It appears you skipped over the padding between vendor length and number of formats... #6

Closed
mrtodd opened this issue Jun 21, 2023 · 5 comments

Comments

@mrtodd
Copy link

mrtodd commented Jun 21, 2023

I could be wrong, but looking at the X documentation there appears to be padding between the vendor length and the number of formats (reference: your x11_send_handshake function). Since the vendor string was likely "The X.Org Foundation" which is 5*4 bytes long, no padding was required for this case.

I was trying to use gdb to look at this, but it complains about no line number information in the executable. I compiled with '-g' so I expected all the required debug info to be there. Do you know what might be the problem? I'm using the following: NASM version 2.15.05 / GNU ld (GNU Binutils for Ubuntu) 2.38 / GNU gdb (Ubuntu 12.1-0ubuntu1~22.04) 12.1 in case it is a version specific problem.

I ended up doing a system call to write the vendor string to STDOUT.

-- Todd

@gaultier
Copy link
Owner

gaultier commented Jun 23, 2023

Good catch, the docs explicitly mention the padding:

     v     STRING8                         vendor
     p                                     unused, p=pad(v)        -----> Padding there
     8n     LISTofFORMAT                   pixmap-formats 

I'll try to make a fix in the next days.

It boils down to padding to the next multiple of 4.

libX11 uses this macro:

#define ROUNDUP(nbytes, pad) (((nbytes) + ((pad)-1)) & ~(long)((pad)-1))

and gcc optimizes it to:

  add     eax, 3
  and     eax, -4

@mrtodd
Copy link
Author

mrtodd commented Jun 23, 2023

I am curious if gdb works for you. Since I'm not getting line number information into my output ELF64, it just runs to the end of the program when I try to single step through the code and the only place I can set breakpoints is at labels. I guess I could just put a bunch of labels into the code around the part I want to look at.

This is what I did to see the vendor string in your "x11_send_handshake" function:

. . .
;; mov eax, DWORD [rdi] ; Store (and return) the window root id.
mov r10d, DWORD [rdi] ; Store (and return) the window root id.

; Set the root_visual_id globally.
mov edx, DWORD [rdi + 32]
mov DWORD [root_visual_id], edx

; DEBUG -- output vendor string
mov rax, SYSCALL_WRITE ; this smashes the result in eax so used r10d as a temp
lea rsi, [rsp + 32]
mov BYTE [rsi + rcx], 10 ; append newline to vendor string
mov rdi, STDOUT
mov rdx, rcx
add rdx, 1 ; increment count for newline char
syscall

mov eax, r10d

add rsp, 1<<15
pop rbp
ret

-- Todd

@gaultier
Copy link
Owner

gaultier commented Jun 26, 2023

Gdb (version 13.2-1) does work for me out of the box including with line numbers:

(gdb) b x11.nasm:111
Breakpoint 1 at 0x401096: file x11.nasm, line 111.
(gdb) r
Starting program: /home/pg/my-code/nasm-work/x11.exe

Breakpoint 1, x11_send_handshake () at x11.nasm:111
111	  syscall
(gdb) n
113	  cmp rax, 12*8 ; Check that all bytes were written.
(gdb) p * (char*)$rsp@12
$1 = "l\000\v\000\000\000\000\000\000\000\000"
(gdb)

Alternatively, breaking on the function name (i.e.: label) should also work but be a bit more cumbersome.

If all fails, gdb can also add a breakpoint at a certain address so one could disassemble like this (assuming no Position Independent Code):

objdump --prefix-addresses -M intel -S x11.exe 

and note at which addresses the interesting code locations are e.g. 0x401098. Then in gdb:

break *0x401098
r
info registers 

Perhaps make sure you compile with nasm -g to generate the debug information, and are not stripping this information at the linking stage with ld -s ?

@gaultier
Copy link
Owner

Fixed in fdead46

@mrtodd
Copy link
Author

mrtodd commented Jun 30, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants