You are on page 1of 6

High Performance Computing

August Assignment
- Iresh Agrawal (SR No. 06-02-01-10-51-16-1-13865), M.Tech (CDS)
1) Find out the details of a.out format for the hello_world.c program.
An a.out file consists of up to seven sections, in the following order:
exec header contains parameters used by the kernel to load a binary file into memory and execute
it, and by the link editor Id to combine a binary file with other binary files; this section is the only
mandatory one.
text segment contains machine code and related data that are loaded into memory when a
program executes; may be loaded read-only.
data segment contains initialized data; always loaded into writable memory.
text relocations contains records used by the link editor to update pointers in the text segment
when combining binary files.
data relocations like the text relocation section, but for data segment pointers.
symbol table contains records used by the link editor to cross-reference the addresses of named
variables and functions (symbols) between binary files.
string table contains the character strings corresponding to the symbol names.
$ readelf -a a.out
ELFHeader:
Magic:7f454c46020101000000000000000000
Class:ELF64
Data:2'scomplement,littleendian
Version:1(current)
OS/ABI:UNIXSystemV
ABIVersion:0
Type:EXEC(Executablefile)
Machine:AdvancedMicroDevicesX8664
Version:0x1
Entrypointaddress:0x400440
Startofprogramheaders:64(bytesintofile)
Startofsectionheaders:4456(bytesintofile)
Flags:0x0
Sizeofthisheader:64(bytes)
Sizeofprogramheaders:56(bytes)
Numberofprogramheaders:9
Sizeofsectionheaders:64(bytes)
Numberofsectionheaders:28
Sectionheaderstringtableindex:27
SectionHeaders:
[Nr]NameTypeAddressOffset
SizeEntSizeFlagsLinkInfoAlign
[0]NULL000000000000000000000000
00000000000000000000000000000000000
[1].interpPROGBITS000000000040023800000238
000000000000001c0000000000000000A001
[2].note.ABItagNOTE000000000040025400000254
00000000000000200000000000000000A004
[3].note.gnu.buildiNOTE000000000040027400000274
00000000000000240000000000000000A004
[4].gnu.hashGNU_HASH000000000040029800000298
000000000000001c0000000000000000A508
[5].dynsymDYNSYM00000000004002b8000002b8
00000000000000600000000000000018A618
[6].dynstrSTRTAB000000000040031800000318
000000000000003d0000000000000000A001
[7].gnu.versionVERSYM000000000040035600000356
00000000000000080000000000000002A502
[8].gnu.version_rVERNEED000000000040036000000360
00000000000000200000000000000000A618
[9].rela.dynRELA000000000040038000000380
00000000000000180000000000000018A508
[10].rela.pltRELA000000000040039800000398

00000000000000480000000000000018A5128
[11].initPROGBITS00000000004003e0000003e0
000000000000001a0000000000000000AX004
[12].pltPROGBITS000000000040040000000400
00000000000000400000000000000010AX0016
[13].textPROGBITS000000000040044000000440
00000000000001820000000000000000AX0016
[14].finiPROGBITS00000000004005c4000005c4
00000000000000090000000000000000AX004
[15].rodataPROGBITS00000000004005d0000005d0
00000000000000100000000000000000A004
[16].eh_frame_hdrPROGBITS00000000004005e0000005e0
00000000000000340000000000000000A004
[17].eh_framePROGBITS000000000040061800000618
00000000000000f40000000000000000A008
[18].init_arrayINIT_ARRAY0000000000600e1000000e10
00000000000000080000000000000000WA008
[19].fini_arrayFINI_ARRAY0000000000600e1800000e18
00000000000000080000000000000000WA008
[20].jcrPROGBITS0000000000600e2000000e20
00000000000000080000000000000000WA008
[21].dynamicDYNAMIC0000000000600e2800000e28
00000000000001d00000000000000010WA608
[22].gotPROGBITS0000000000600ff800000ff8
00000000000000080000000000000008WA008
[23].got.pltPROGBITS000000000060100000001000
00000000000000300000000000000008WA008
[24].dataPROGBITS000000000060103000001030
00000000000000100000000000000000WA008
[25].bssNOBITS000000000060104000001040
00000000000000080000000000000000WA001
[26].commentPROGBITS000000000000000000001040
000000000000002b0000000000000001MS001
[27].shstrtabSTRTAB00000000000000000000106b
00000000000000f80000000000000000001
KeytoFlags:
W(write),A(alloc),X(execute),M(merge),S(strings),l(large)
I(info),L(linkorder),G(group),T(TLS),E(exclude),x(unknown)
O(extraOSprocessingrequired)o(OSspecific),p(processorspecific)
Therearenosectiongroupsinthisfile.
ProgramHeaders:
TypeOffsetVirtAddrPhysAddr
FileSizMemSizFlagsAlign
PHDR0x00000000000000400x00000000004000400x0000000000400040
0x00000000000001f80x00000000000001f8RE8
INTERP0x00000000000002380x00000000004002380x0000000000400238
0x000000000000001c0x000000000000001cR1
[Requestingprograminterpreter:/lib64/ldlinuxx8664.so.2]
LOAD0x00000000000000000x00000000004000000x0000000000400000
0x000000000000070c0x000000000000070cRE200000
LOAD0x0000000000000e100x0000000000600e100x0000000000600e10
0x00000000000002300x0000000000000238RW200000
DYNAMIC0x0000000000000e280x0000000000600e280x0000000000600e28
0x00000000000001d00x00000000000001d0RW8
NOTE0x00000000000002540x00000000004002540x0000000000400254
0x00000000000000440x0000000000000044R4
GNU_EH_FRAME0x00000000000005e00x00000000004005e00x00000000004005e0
0x00000000000000340x0000000000000034R4
GNU_STACK0x00000000000000000x00000000000000000x0000000000000000
0x00000000000000000x0000000000000000RW10
GNU_RELRO0x0000000000000e100x0000000000600e100x0000000000600e10
0x00000000000001f00x00000000000001f0R1
SectiontoSegmentmapping:
SegmentSections...
00
01.interp
02.interp.note.ABItag.note.gnu.buildid.gnu.hash.dynsym.dynstr.gnu.version.gnu.version_r.rela.dyn
.rela.plt.init.plt.text.fini.rodata.eh_frame_hdr.eh_frame
03.init_array.fini_array.jcr.dynamic.got.got.plt.data.bss
04.dynamic
05.note.ABItag.note.gnu.buildid
06.eh_frame_hdr
07
08.init_array.fini_array.jcr.dynamic.got
Dynamicsectionatoffset0xe28contains24entries:
TagTypeName/Value
0x0000000000000001(NEEDED)Sharedlibrary:[libc.so.6]
0x000000000000000c(INIT)0x4003e0
0x000000000000000d(FINI)0x4005c4
0x0000000000000019(INIT_ARRAY)0x600e10
0x000000000000001b(INIT_ARRAYSZ)8(bytes)
0x000000000000001a(FINI_ARRAY)0x600e18

0x000000000000001c(FINI_ARRAYSZ)8(bytes)
0x000000006ffffef5(GNU_HASH)0x400298
0x0000000000000005(STRTAB)0x400318
0x0000000000000006(SYMTAB)0x4002b8
0x000000000000000a(STRSZ)61(bytes)
0x000000000000000b(SYMENT)24(bytes)
0x0000000000000015(DEBUG)0x0
0x0000000000000003(PLTGOT)0x601000
0x0000000000000002(PLTRELSZ)72(bytes)
0x0000000000000014(PLTREL)RELA
0x0000000000000017(JMPREL)0x400398
0x0000000000000007(RELA)0x400380
0x0000000000000008(RELASZ)24(bytes)
0x0000000000000009(RELAENT)24(bytes)
0x000000006ffffffe(VERNEED)0x400360
0x000000006fffffff(VERNEEDNUM)1
0x000000006ffffff0(VERSYM)0x400356
0x0000000000000000(NULL)0x0
Relocationsection'.rela.dyn'atoffset0x380contains1entries:
OffsetInfoTypeSym.ValueSym.Name+Addend
000000600ff8000300000006R_X86_64_GLOB_DAT0000000000000000__gmon_start__+0
Relocationsection'.rela.plt'atoffset0x398contains3entries:
OffsetInfoTypeSym.ValueSym.Name+Addend
000000601018000100000007R_X86_64_JUMP_SLO0000000000000000puts+0
000000601020000200000007R_X86_64_JUMP_SLO0000000000000000__libc_start_main+0
000000601028000300000007R_X86_64_JUMP_SLO0000000000000000__gmon_start__+0
ThedecodingofunwindsectionsformachinetypeAdvancedMicroDevicesX8664isnotcurrentlysupported.
Symboltable'.dynsym'contains4entries:
Num:ValueSizeTypeBindVisNdxName
0:00000000000000000NOTYPELOCALDEFAULTUND
1:00000000000000000FUNCGLOBALDEFAULTUNDputs@GLIBC_2.2.5(2)
2:00000000000000000FUNCGLOBALDEFAULTUND__libc_start_main@GLIBC_2.2.5(2)
3:00000000000000000NOTYPEWEAKDEFAULTUND__gmon_start__
Versionsymbolssection'.gnu.version'contains4entries:
Addr:0000000000400356Offset:0x000356Link:5(.dynsym)
000:0(*local*)2(GLIBC_2.2.5)2(GLIBC_2.2.5)0(*local*)
Versionneedssection'.gnu.version_r'contains1entries:
Addr:0x0000000000400360Offset:0x000360Link:6(.dynstr)
000000:Version:1File:libc.so.6Cnt:1
0x0010:Name:GLIBC_2.2.5Flags:noneVersion:2
Displayingnotesfoundatfileoffset0x00000254withlength0x00000020:
OwnerDatasize Description
GNU0x00000010 NT_GNU_ABI_TAG(ABIversiontag)
OS:Linux,ABI:2.6.24
Displayingnotesfoundatfileoffset0x00000274withlength0x00000024:
OwnerDatasize Description
GNU0x00000014 NT_GNU_BUILD_ID(uniquebuildIDbitstring)
BuildID:9d162a1eec5befad5ce53e6fc4a19c9c5deaeb87

2) What is the size of the code segment in a.out for the hello_world.c program?
1214 bytes.
$ size a.out
text data
bss
dec
hex filename
1214 560
8
1782 6f6 a.out
text is my code, vector table plus constants.
data is for initialized variables, and it counts for RAM and FLASH. The linker allocates the data
in FLASH which then is copied from ROM to RAM in the startup code.
bss is for the uninitialized data in RAM which is initialized with zero in the startup code.
3) What is the size of a.out for the hello_world.c program ?
6248 bytes
4) Check if your system supports IEEE format. Write a program to check arthmetic on NaN,
Denormals and Infinity?
#include<stdio.h>

intmain()
{
unsignedchar*c;
floatzero=0;
c=&zero;
//Hexformof0is0x00000000(s=0,e=0,f=0)
if((*c==0x00)&&(*(c+1)==0x00)&&(*(c+2)==0x00)&&(*(c+3)==0x00))
printf("\nIEEE754floatingpointrepresentationof0iscorrect");
else
printf("\nIEEE754floatingpointrepresentationof0isincorrect");
floatone=1;
c=&one;
//Hexformof1is0x3f800000andformatisLittleEndian(s=0,e=127,f=0)
if((*c==0x00)&&(*(c+1)==0x00)&&(*(c+2)==0x80)&&(*(c+3)==0x3f))
printf("\nIEEE754floatingpointrepresentationof1iscorrect");
else
printf("\nIEEE754floatingpointrepresentationof1isincorrect");
floatspn,lpn;
c=&spn;
//Smallestpositiveno.innormalizedformis0x00800000(s=0,e=1,f=0)
*c=0x00;*(c+1)=0x00;*(c+2)=0x80;*(c+3)=0x00;
printf("\nSmallestpositiveno.innormalizedforminIEEE754floatingpoint
representationis%e",spn);
c=&lpn;
//Largestpositiveno.innormalizedformis0x7f7fffff(s=0,e=254,f=12^(
23))
*c=0xff;*(c+1)=0xff;*(c+2)=0x7f;*(c+3)=0x7f;
printf("\nLargestpositiveno.innormalizedforminIEEE754floatingpoint
representationis%e",lpn);

floatspdn,lpdn;
c=&spdn;
//Smallestpositiveno.indenormalizedformis0x00000001(s=0,e=0,f=2^(23))
*c=0x01;*(c+1)=0x00;*(c+2)=0x00;*(c+3)=0x00;
printf("\nSmallestpositiveno.indenormalizedforminIEEE754floatingpoint
representationis%e",spdn);
c=&lpdn;
//Largestpositiveno.indenormalizedformis0x007fffff(s=0,e=0,f=12^(
23))
*c=0xff;*(c+1)=0xff;*(c+2)=0x7f;*(c+3)=0x00;
printf("\nLargestpositiveno.indenormalizedforminIEEE754floatingpoint
representationis%e",lpdn);
floatpinf,ninf;
c=&pinf;
//Positiveinfinityis0x7f800000(s=0,e=255,f=0)
*c=0x00;*(c+1)=0x00;*(c+2)=0x80;*(c+3)=0x7f;
printf("\nPositiveinfinityinIEEE754floatingpointrepresentationis
%e",pinf);
c=&ninf;
//Negativeinfinityis0xff800000(s=1,e=255,f=0)
*c=0x00;*(c+1)=0x00;*(c+2)=0x80;*(c+3)=0xff;
printf("\nNegativeinfinityinIEEE754floatingpointrepresentationis
%e",ninf);
floatnan;
c=&nan;
//Notanumberis0x7f800001(s=0,e=255,f=1)
*c=0x01;*(c+1)=0x00;*(c+2)=0x80;*(c+3)=0x7f;
printf("\nNotanumberinIEEE754floatingpointrepresentationis%e",nan);
floata=1,b=0;//CheckingarithmeticonNaN,denormalsandinfinity
printf("\n1/0=%e",a/b);
printf("\n1/0=%e",a/b);

printf("\n0/0=%e",b/b);
printf("\n(1/0)+(1/0)=%e",a/b+a/b);
printf("\n(1/0)(1/0)=%e",a/ba/b);
printf("\n1e44=%e",(float)1e44);
printf("\n1e45=%e",(float)1e45);
return0;
}

5) Practice Problem 2.43


In the following code, we have omitted the definitions of constants M and N:
#define M /* Mystery number 1 */
#define N /* Mystery number 2 */
int arith(int x, int y) {
int result = 0;
result = x*M + y/N; /* M and N are mystery numbers. */
return result;
}
/* Translation of assembly code for arith */
int optarith(int x, int y) {
int t = x;
x <<= 5;
x -= t;
if (y < 0) y += 7;
y >>= 3; /* Arithmetic shift */
return x+y;
}
What are the values of M and N?
x*M = x*32-x = x*31. So, M = 31.
N is 8; a bias value of 7 is added when y is negative, and the right shift is by 3.
6) The imprecision of floating-point arithmetic can have disastrous effects.
The Patriot system contains an internal clock, implemented as a counter that is incremented every
0.1 seconds. To determine the time in seconds, the program would multiply the value of this counter
by a 24-bit quantity that was a fractional binary approximation to 1/10 . In particular, the binary
representation of 1/10 is the nonterminating sequence 0.000110011[0011]...2, where the portion in
brackets is repeated indefinitely. The program approximated 0.1, as a value x, by considering just
the first 23 bits of the sequence to the right of the binary point:
x = 0.00011001100110011001100.
A. What is the binary representation of 0.1 x?
The binary representation of 0.1-x is 0.000000000000000000000001100[1100], i.e., 23 zeroes to
the right of the binary point followed by the repeating sequence [1100].
B. What is the approximate decimal value of 0.1 x?
2^-24 + 2^-25 + 2^-28 + 2^-29 + 2^-32 + 2^-33 + 2^-36 + 2^-37 = 9.53659764e-8
C. The clock starts at 0 when the system is first powered up and keeps counting up from there. In
this case, the system had been running for around 100 hours. What was the difference between the
actual time and the time computed by the software?
9.53659764e-8 * 3600000 = 0.34331751521s
D. The system predicts where an incoming missile will appear based on its velocity and the time of

the last radar detection. Given that a Scud travels at around 2000 meters per second, how far off
was its prediction?
0.34331751521s * 2000m/s = 686.635030434m
7) Practice Problem 2.49
A. For a floating-point format with an n-bit fraction, give a formula for the smallest positive integer
that cannot be represented exactly (because it would require an n+1-bit fraction to be exact).
Assume the exponent field size k is large enough that the range of representable exponents does not
provide a limitation for this problem.
2^(n+1) + 1
B. What is the numeric value of this integer for single-precision format (n = 23)?
2^24 + 1 = 16777217
8)Practice Problem 2.53
Fill in the following macro definitions to generate the double-precision values +,
, and 0:
#define POS_INFINITY
#define NEG_INFINITY
#define NEG_ZERO
You cannot use any include files (such as math.h), but you can make use of the fact that the largest
finite number that can be represented with double precision is around 1.8 10^308
Assuming the machine supports IEEE 754 format fully.
#define POS_INFINITY 1e310
#define NEG_INFINITY -POS_INFINITY
#define NEG_ZERO -1/POS_INFINITY
9)Practice Problem 2.48
As mentioned in Problem 2.6, the integer 3,510,593 has hexadecimal representation 0x00359141,
while the single-precision, floating-point number 3510593.0 has hexadecimal representation
0x4A564504. Derive this floating-point representation and explain the correlation between the bits
of the integer and floating-point representations.
3510593.0 in binary = 1101011001000101000001 = 1.101011001000101000001*2^21
Sign = 0. Fraction = 101011001000101000001. Exponent = 21 + 127 = 148 = 10010100
[0 10010100 10101100100010100000100]
3510593.0 in binary =
1101011001000101000001
3510593.0 in FPN = 0 10010100 10101100100010100000100
2.62 Write a function int_shifts_are_arithmetic() that yields 1 when run on a machine that uses
arithmetic right shifts for ints, and 0 otherwise. Your code should work on a machine with any word
size.
boolint_shifts_are_arithmetic()
{
inti;
i=1;
i>>2;
if(i==1)
returntrue;
returnfalse;
}