1

In a core dump file, I am able to view environmental variables with:

$ gdb file core
>>> p __environ[0]
$1 = 0x7ffe6420dcbd "SHELL=/bin/bash"
>>> p __environ[1]
$2 = 0x7ffe6420dccd "TMUX=/tmp/tmux-1001/default,13871,0"
# basically the same as doing $ printenv    

However, when I try and debug a normal executable:

# file.s
.globl main
main:
    ret

$ gcc file.s -ggdb3 -o file2
$ gdb -q file2
Reading symbols from file...
(No debugging symbols found in file)
>>> starti
>>> p __environ
No symbol "__environ" in current context.

How can I get access to the environ and argv ? One possible way is to examine its process file:

>>> python print(open('/proc/48011/environ').read().replace('\x00','\n'))
SHELL=/bin/bash
TMUX=/tmp/tmux-1001/default,13871,0
SSH_AUTH_SOCK=/tmp/ssh-RtmbGp8HSFcN/agent.14219
SSH_AGENT_PID=14220
EDITOR=/usr/bin/vim

Where 48011 is from:

─── Threads ───────────────────────────────────────────────────────────────────
[1] id 48011 name file from 0x0000000000401005 in _start 
David542
  • 104,438
  • 178
  • 489
  • 842
  • Please show how you built the executable, how you ran gdb and the gdb commands you ran (if any) leading up to that. That is, provide a [minimal verifiable example](https://stackoverflow.com/help/minimal-reproducible-example). You are probably missing some steps but can't say what unless you provide more details of what you actually did. – kaylum Oct 06 '20 at 03:30
  • I suspect you just started gdb but did not actually run the executable. But that's just a guess based on the symptoms. Provide more info for a more definete answer. – kaylum Oct 06 '20 at 03:34
  • @kaylum thanks for the feedback, I updated the question, also seems it may be related to: https://stackoverflow.com/a/32917097/651174 – David542 Oct 06 '20 at 03:40
  • Is that your actual and complete gdb output? Or did you trim some of it? I'm expecting to see more than that. Can you show the full output? – kaylum Oct 06 '20 at 03:43
  • @kaylum sure, for which code section, the second one where it says No symbol table? – David542 Oct 06 '20 at 03:51
  • everything from `gdb file` onwards. – kaylum Oct 06 '20 at 03:52
  • @kaylum k updated. – David542 Oct 06 '20 at 03:59
  • @kaylum by the way, now it gives a different message. – David542 Oct 06 '20 at 04:01

1 Answers1

1

According to its documentation, GDB has a show environment command. On Linux, read also gdb(1), gcore(1), core(5), proc(5), signal(7) and gcc(1) and conventions documented in environ(7). See also getenv(3) and putenv(3). You could also play with objdump(1) and readelf(1) and nm(1). Be sure to read more documentation about ELF, perhaps in the Linkers and loaders book.

But if you compile on LInux an assembler file using gcc file.s -ggdb3 -o file2 that assembler file lacks DWARF debug information, nearly required by GDB.

So try writing a small hello-world.c program, compile it using gcc -Wall -Wextra -fverbose-asm -g -S hello-world.c and examine (e.g. with less(1)) the emitted hello-world.s assembler file. You'll see there a lot of DWARF related assembler directives. Compile it again with gcc -v -Wall -Wextra -g -O hello-world.c -o helloworld to understand what programs GCC is starting for you. Read also the documentation about invoking GCC.

Be of course aware of crt0 startup code. On Linux, study the source code of both GNU libc (or musl-libc) and of GCC and binutils. All of them are free software or at least open source software, and you are allowed to download, study and improve their source code.

Read also the specification of Linux ABI. See this answer. Consider also studying the source code of your Linux kernel. See also kernelnewbies.

You might be interested by linuxfromscratch.org which gives detailed step by step instructions (to build an entire Linux distribution from scratch). Read Advanced Linux Programming and Linux Assembler HowTo. Budget then several weeks of work.

Read also execve(2), elf(5), syscalls(2) and study the source code of GNU bash.

Read wikipages about processes, system calls, computer files, file systems and executables. Consider reading a good textbook on operating systems and the Modern C book and perhaps the C11 standard n1570.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • good idea thanks for the help. I'll try with `-ggdb3` too. One question: when running an assembly program, do the environmental variables get added to the stack in the program memory? Or is this done as part of the c-startup code, and would not occur in hand-written assembly? – David542 Oct 06 '20 at 04:10
  • My opinion is that several dozens of pages are required to answer that question. Please follow all the hyperlinks given in my answer – Basile Starynkevitch Oct 06 '20 at 04:28
  • thanks for the thorough answer I will check them out. In short though, assembly code written by itself does not add system environments, correct? – David542 Oct 06 '20 at 04:33
  • Sorry, you need to read the hyperlinks I have given in my answer above, or else contact me by email to `basile@starynkevitch.net` mentioning the URL of your question – Basile Starynkevitch Oct 06 '20 at 04:35
  • Assembler code are passive textual files, conventionally ended with `.s`. Of course they don't change the system environment. Only processes can change them – Basile Starynkevitch Oct 06 '20 at 04:48