임베디드 기초-0709

volatile 쓰는 이유

실시간으로 변하는 값을 가져와야 할때, 컴파일러는 같은주소에서 값을 가져 오려고 하면안함.때문에 최적화를 시키면 안됨!

READ A
PRINT A
READ A
PRINT A
...
compiile optimize
...>

READ A
PRINT A
PRINT A
//READ A를 지워버리는 이유를 같은 주소에서 값을 가져오기떄문에 지우는것!(같은 일을 하지 않기 위해서)
// 근데 실상 A가 실시간으로 변하는 값이라서 매번 가져와야 하는데 저렇게 안가져오면 같은 값이 나오게된다.
// 그래서 volatile이 쓰인다.

// Using volatile
volatile A
READ A
PRINT A
READ A
PRINT A
...
compiile optimize
...>
READ A
PRINT A
READ A
PRINT A

gcc-arm tool chain 설치방법

mac용 다운로드 GNU Arm Embedded Toolchain

$ mkdir /usr/local/gcc-arm
$ tar -xjf gcc-arm-none-eabi-7-2018-q2-update-mac.tar.tar.bz2 -C /usr/local/gcc-arm
# /usr/local/gcc-arm 안에 설치되어 있다.
$ sudo chmode -R -w /usr/local/gcc-arm
# 설치 확인
$ arm-none-eabi-gcc --version
arm-none-eabi-gcc (GNU Tools for Arm Embedded Processors 7-2018-q2-update) 7.3.1 20180622 (release) [ARM/embedded-7-branch revision 261907]
Copyright (C) 2017 Free Software Foundation, Inc.

homebrew 설치 방법(되는건지 잘 모르겠음)

$ brew tap PX4/homebrew-px4
$ brew update
$ brew install gcc-arm-none-eabi

맥용 objdump 설치 방법

objdump는 deassemble을 해서 분석하기 위해 사용된다. Mac 에서는 otool 이라는것이 존재하는데 어떻게 사용하는지 잘모르겠고, objdump보다 더 불친절 하게 출력해준다.

$ brew install binutils
$ gobjdump
# /usr/local/Cellar/binutils/2.30 에 가보면 objdump가 아니라
# gobjdump로 되어있다 아마 맥에서 prefix가 바뀌어서 그런듯
$ vi ~/.bashrc
# In .bashrc  다음 내용 추가
export PATH="/usr/local/gcc-arm/gcc-arm-none-eabi-7-2018-q2-update/arm-none-eabi/bin:/usr/local/gcc-arm/gcc-arm-none-eabi-7-2018-q2-update/bin:$PATH"


$ arm-none-eabi-gcc --version
arm-none-eabi-gcc (GNU Tools for Arm Embedded Processors 7-2018-q2-update) 7.3.1 20180622 (release) [ARM/embedded-7-branch revision 261907]
Copyright (C) 2017 Free Software Foundation, Inc.

ex 예제

ex 폴더에서 make하면 image가 만들어짐

$ cd /ex
$ vi Makefile
VERSION     = 7.3.1
//7.3.1 버전이라 저렇게 수정해야댐
TOOLPATH    = /usr/local/gcc-arm/gcc-arm-none-eabi-7-2018-q2-update
//이렇게 수정함.
//arm-none-eabi-gcc (GNU Tools for Arm Embedded Processors 7-2018-q2-update) //7.3.1 20180622 (release) [ARM/embedded-7-branch revision 261907]
//Copyright (C) 2017 Free Software Foundation, Inc.
$ vi startup.S
#msr        FPEXC,r0  # 여기 부분 주석 처리해야댐 자구 오류남
$ make
$ ls
...
image image.bin image.map 세가지가 생김
...

$ greadelf -S image
There are 16 section headers, starting at offset 0x94e0:

Section Headers:
  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al
  [ 0]                   NULL            00000000 000000 000000 00      0   0  0
  [ 1] .text             PROGBITS        20000000 000058 001f8c 00  AX  0   0  4
  [ 2] .rodata           PROGBITS        20001f8c 001fe4 000014 00   A  0   0  4
  [ 3] .data             PROGBITS        20001fa0 001ff8 000020 00  WA  0   0  8
  [ 4] .bss              NOBITS          20001fc0 002018 0017b8 00  WA  0   0  8
  [ 5] .debug_info       PROGBITS        00000000 002018 0019c2 00      0   0  1
  [ 6] .debug_abbrev     PROGBITS        00000000 0039da 000457 00      0   0  1
  [ 7] .debug_aranges    PROGBITS        00000000 003e38 000080 00      0   0  8
  [ 8] .debug_line       PROGBITS        00000000 003eb8 000955 00      0   0  1
  [ 9] .debug_str        PROGBITS        00000000 00480d 00094c 01  MS  0   0  1
  [10] .comment          PROGBITS        00000000 005159 00007f 01  MS  0   0  1
  [11] .ARM.attributes   ARM_ATTRIBUTES  00000000 0051d8 000039 00      0   0  1
  [12] .debug_frame      PROGBITS        00000000 005214 0004c4 00      0   0  4
  [13] .symtab           SYMTAB          00000000 0056d8 0027f0 10     14 468  4
  [14] .strtab           STRTAB          00000000 007ec8 00157e 00      0   0  1
  [15] .shstrtab         STRTAB          00000000 009446 00009a 00      0   0  1
Key to Flags:
  W (write), A (alloc), X (execute), M (merge), S (strings), I (info),
  L (link order), O (extra OS processing required), G (group), T (TLS),
  C (compressed), x (unknown), o (OS specific), E (exclude),
  y (purecode), p (processor specific)



$ size image
   text    data     bss     dec     hex filename
   8076      52    6072   14200    3778 image
$ ls -l image.bin
-rwxr-xr-x  1 root  staff  8128  7  9 16:51 image.bin

hello.c 예제

$ gcc -E hello.c
$ gcc -S hello.c
$ gcc -c hello.s
//이때 hello.o 파일이 생성된다.
$ file hello.o
hello.o: Mach-O 64-bit object x86_64
$ cc hello.o
$ ls
...
a.out
...
$ ./a.out
hello

오브젝트 파일과 실행 파일의 차이 공부!

//오브젝트 파일의 내용을 살펴보자
$ gobjdump -d hello.o
hello.o:     file format mach-o-x86-64
hello.o
architecture: i386:x86-64, flags 0x00000011:
HAS_RELOC, HAS_SYMS
start address 0x0000000000000000

Sections:
Idx Name          Size      VMA               LMA               File off  Algn
  0 .text         00000031  0000000000000000  0000000000000000  00000270  2**4
                  CONTENTS, ALLOC, LOAD, RELOC, CODE
  1 .data         00000004  0000000000000034  0000000000000034  000002a4  2**2
                  CONTENTS, ALLOC, LOAD, DATA
  2 .cstring      00000007  0000000000000038  0000000000000038  000002a8  2**0
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  3 __LD.__compact_unwind 00000020  0000000000000040  0000000000000040  000002b0  2**3
                  CONTENTS, RELOC, DEBUGGING
  4 .eh_frame     00000040  0000000000000060  0000000000000060  000002d0  2**3
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
SYMBOL TABLE:
0000000000000034 g       0f SECT   02 0000 [.data] _a
0000000000000000 g       0f SECT   01 0000 [.text] _main
0000000000000004         01 COM    00 0200 _b
0000000000000028         01 COM    00 0400 _c
0000000000000000 g       01 UND    00 0000 _printf


RELOCATION RECORDS FOR [.text]:
OFFSET           TYPE              VALUE 
0000000000000020 BRANCH32          _printf
000000000000000b DISP32            .cstring-0x0000000000000038


RELOCATION RECORDS FOR [__LD.__compact_unwind]:
OFFSET           TYPE              VALUE 
0000000000000000 64                .text
//이전의 오브젝트 파일과 실행 파일의 비교
$ gobjdump -d a.out
a.out:     file format mach-o-x86-64
a.out
architecture: i386:x86-64, flags 0x00000012:
EXEC_P, HAS_SYMS
start address 0x0000000100000f50

Sections:
Idx Name          Size      VMA               LMA               File off  Algn
  0 .text         00000031  0000000100000f50  0000000100000f50  00000f50  2**4
                  CONTENTS, ALLOC, LOAD, CODE
  1 __TEXT.__stubs 00000006  0000000100000f82  0000000100000f82  00000f82  2**1
                  CONTENTS, ALLOC, LOAD, READONLY, CODE
  2 __TEXT.__stub_helper 0000001a  0000000100000f88  0000000100000f88  00000f88  2**2
                  CONTENTS, ALLOC, LOAD, READONLY, CODE
  3 .cstring      00000007  0000000100000fa2  0000000100000fa2  00000fa2  2**0
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  4 __TEXT.__unwind_info 00000048  0000000100000fac  0000000100000fac  00000fac  2**2
                  CONTENTS, ALLOC, LOAD, READONLY, CODE
  5 __DATA.__nl_symbol_ptr 00000010  0000000100001000  0000000100001000  00001000  2**3
                  CONTENTS, ALLOC, LOAD, DATA
  6 __DATA.__la_symbol_ptr 00000008  0000000100001010  0000000100001010  00001010  2**3
                  CONTENTS, ALLOC, LOAD, DATA
  7 .data         00000004  0000000100001018  0000000100001018  00001018  2**2
                  CONTENTS, ALLOC, LOAD, DATA
  8 __DATA.__common 00000038  0000000100001020  0000000100001020  00000000  2**4
                  ALLOC
SYMBOL TABLE:
0000000100000000 g       0f SECT   01 0010 [.text] __mh_execute_header
0000000100001018 g       0f SECT   08 0000 [.data] _a
0000000100001020 g       0f SECT   09 0000 [__DATA.__common] _b
0000000100001030 g       0f SECT   09 0000 [__DATA.__common] _c
0000000100000f50 g       0f SECT   01 0000 [.text] _main
0000000000000000 g       01 UND    00 0100 _printf
0000000000000000 g       01 UND    00 0100 dyld_stub_binder
$ xxd hello.o  => 이진수 파일을 16진수로 출력해줌 -r을 주면 반대로
00000000: cffa edfe 0700 0001 0300 0000 0100 0000  ................
00000010: 0400 0000 0002 0000 0020 0000 0000 0000  ......... ......
00000020: 1900 0000 8801 0000 0000 0000 0000 0000  ................
00000030: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000040: 9800 0000 0000 0000 2002 0000 0000 0000  ........ .......
00000050: 9800 0000 0000 0000 0700 0000 0700 0000  ................
00000060: 0400 0000 0000 0000 5f5f 7465 7874 0000  ........__text..
00000070: 0000 0000 0000 0000 5f5f 5445 5854 0000  ........__TEXT..
00000080: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000090: 2a00 0000 0000 0000 2002 0000 0400 0000  *....... .......
000000a0: b802 0000 0200 0000 0004 0080 0000 0000  ................
000000b0: 0000 0000 0000 0000 5f5f 6373 7472 696e  ........__cstrin
000000c0: 6700 0000 0000 0000 5f5f 5445 5854 0000  g.......__TEXT..
000000d0: 0000 0000 0000 0000 2a00 0000 0000 0000  ........*.......
000000e0: 0700 0000 0000 0000 4a02 0000 0000 0000  ........J.......
000000f0: 0000 0000 0000 0000 0200 0000 0000 0000  ................
00000100: 0000 0000 0000 0000 5f5f 636f 6d70 6163  ........__compac
00000110: 745f 756e 7769 6e64 5f5f 4c44 0000 0000  t_unwind__LD....
00000120: 0000 0000 0000 0000 3800 0000 0000 0000  ........8.......
00000130: 2000 0000 0000 0000 5802 0000 0300 0000   .......X.......
00000140: c802 0000 0100 0000 0000 0002 0000 0000  ................
00000150: 0000 0000 0000 0000 5f5f 6568 5f66 7261  ........__eh_fra
00000160: 6d65 0000 0000 0000 5f5f 5445 5854 0000  me......__TEXT..
00000170: 0000 0000 0000 0000 5800 0000 0000 0000  ........X.......
00000180: 4000 0000 0000 0000 7802 0000 0300 0000  @.......x.......
00000190: 0000 0000 0000 0000 0b00 0068 0000 0000  ...........h....
000001a0: 0000 0000 0000 0000 2400 0000 1000 0000  ........$.......
000001b0: 000d 0a00 0000 0000 0200 0000 1800 0000  ................
000001c0: d002 0000 0200 0000 f002 0000 1000 0000  ................
000001d0: 0b00 0000 5000 0000 0000 0000 0000 0000  ....P...........
000001e0: 0000 0000 0100 0000 0100 0000 0100 0000  ................
000001f0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000200: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000210: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000220: 5548 89e5 4883 ec10 488d 3d1b 0000 00c7  UH..H...H.=.....
00000230: 45fc 0000 0000 b000 e800 0000 0031 c989  E............1..
00000240: 45f8 89c8 4883 c410 5dc3 6865 6c6c 6f0a  E...H...].hello.
00000250: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000260: 2a00 0000 0000 0001 0000 0000 0000 0000  *...............
00000270: 0000 0000 0000 0000 1400 0000 0000 0000  ................
00000280: 017a 5200 0178 1001 100c 0708 9001 0000  .zR..x..........
00000290: 2400 0000 1c00 0000 88ff ffff ffff ffff  $...............
000002a0: 2a00 0000 0000 0000 0041 0e10 8602 430d  *........A....C.
000002b0: 0600 0000 0000 0000 1900 0000 0100 002d  ...............-
000002c0: 0b00 0000 0200 0015 0000 0000 0100 0006  ................
000002d0: 0100 0000 0f01 0000 0000 0000 0000 0000  ................
000002e0: 0700 0000 0100 0000 0000 0000 0000 0000  ................
000002f0: 005f 6d61 696e 005f 7072 696e 7466 0000  ._main._printf..

objdump 사용법

$ gobjdump -D -x -s -h 실행파일

© 2018 Copyright CodexLab. All rights reserved.

Powered by Jekyll, Designed by Codex.