1. Engineering
  2. Computer Science
  3. consider the following poorly written c code int main ...

Question: consider the following poorly written c code int main ...

Question details

Consider the following poorly written C code:

int main() {
    int n = doCopy();
    printf("%x\n", n); 
}

int doCopy() {
    int n = 1; 
    char dest[8];
    strcpy(dest, "01234567890123456789");
    return n;
}

The gcc compiler (without turning on optimizations) was used to create an executable out of this code and then objdump -d was used to disassemble the executable. The output of the objdump command can be seen below:

0000000000400504 <main>:
  400504:   55                      push   %rbp
  400505:   48 89 e5                mov    %rsp,%rbp
  400508:   48 83 ec 10             sub    $0x10,%rsp
  40050c:   b8 00 00 00 00          mov    $0x0,%eax
  400511:   e8 24 00 00 00          callq  40053a <doCopy>
  400516:   48 89 45 f8             mov    %rax,-0x8(%rbp)
  40051a:   b8 68 06 40 00          mov    $0x400668,%eax
  40051f:   48 8b 55 f8             mov    -0x8(%rbp),%rdx
  400523:   48 89 d6                mov    %rdx,%rsi
  400526:   48 89 c7                mov    %rax,%rdi
  400529:   b8 00 00 00 00          mov    $0x0,%eax
  40052e:   e8 bd fe ff ff          callq  4003f0 <printf@plt>
  400533:   b8 00 00 00 00          mov    $0x0,%eax
  400538:   c9                      leaveq
  400539:   c3                      retq

000000000040053a <doCopy>:
  40053a:   55                      push   %rbp
  40053b:   48 89 e5                mov    %rsp,%rbp
  40053e:   48 83 ec 10             sub    $0x10,%rsp
  400542:   48 c7 45 f8 01 00 00    movq   $0x1,-0x8(%rbp)
  400549:   00 
  40054a:   b9 6c 06 40 00          mov    $0x40066c,%ecx
  40054f:   48 8d 45 f0             lea    -0x10(%rbp),%rax
  400553:   ba 15 00 00 00          mov    $0x15,%edx
  400558:   48 89 ce                mov    %rcx,%rsi
  40055b:   48 89 c7                mov    %rax,%rdi
  40055e:   e8 ad fe ff ff          callq  400410 <memcpy@plt>
  400563:   48 8b 45 f8             mov    -0x8(%rbp),%rax
  400567:   c9                      leaveq
  400568:   c3                      retq

Suppose that right before the doCopy function is called that the values of %rbp and %rsp are as follows:

%rbp: 0x7fffffffe200
%rsp: 0x7fffffffe1f0 

Now, assume that the memcpy function has been executed and the program is about to return from the doCopy function. What is the return address?

a. 1

b. 0x0000000039383736

c. 0x0000000000400516

d. 0x3736353433323130

e. 0x3534333231303938

f. 0x00007f0039383736

please explain for me

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution