Using sed and awk together to create replacement maps

2

I'm attempting to reverse engineer some software to gain a better understanding the lower-level workings of the OS. I want to replace the raw address dumps from objdump -d with the corresponding values from objdump -t, something that even the -Dslx option seems to miss.

the output from objdump -t looks like this:

00000000 l    d  .bss   00000000 .bss
00000000 g       .text  00000000 fred

and I am able to extract the needed information using awk at indexes 1 and NF(the actual code I used is at another computer). For each record I put the fields into an array:

fields[$1] = $NF

and from here I am kind of lost. I want to use these entries to replace all occurrences of 'location' with

fields[location]

At this point I am just printing out the sed command to use next, then manually copying and pasting the command and executing it. it comes out looking like

objdump -d test.o | sed -e 's/<loc1>/<name1>/g' 
                        -e 's/<loc2>/<name2>/g' 
                        . . . 
                        -e 's/<locN>/<nameN>/g'

I don't really like this, I want to completely automate this. I'm relatively inexperienced with some of the Bash tricks so chances are there's a simple solution I've never heard of.

example: (I don't have access to actual inputs at this point, but this is close enough for the goal I have)

00000000 g     O .bbs 00000010 size
00000012 g     O .bbs 00000004 count
00000200 l     F .bbs 00000020 anotherVar

+

0x0000030 <main>:
    0x00000034    xx xx      mov   0x00000000, %eax
    0x00000038    xx         push  0x00000012
    0x0000004c    xx xx xx   subl  0x00000200, %ebx

=

0x0000030 <main>:
    0x00000034    xx xx      mov   size, %eax
    0x00000038    xx         push  count
    0x0000004c    xx xx xx   subl  anotherVar, %ebx
bash
shell
sed
awk
objdump
asked on Stack Overflow Oct 11, 2012 by AdamSpurgin • edited Oct 11, 2012 by AdamSpurgin

1 Answer

1

There probably is a more-sophisticated answer to your question than given here (for example, doing the whole process with a single awk, python, or perl program), but until that answer appears you can automate your current process by using eval as below.

You say you are “printing out the sed command to use next, then manually copying and pasting the command and executing it”. Suppose XYZ is the pipeline that generates and prints the sed command. Instead of using XYZ to print/copy/paste, use it to create the sed command to execute, and then use eval to execute the command:

SEDCOM=$(XYZ)
eval $SEDCOM

An example: If we say
t="echo do do do; echo da da da"
$t
the resulting output is
do do do; echo da da da
but if we instead say
eval $t
the output is
do do do
da da da


User contributions licensed under CC BY-SA 3.0