Union + int within assembly and C Language

Source: Internet
Author: User

Just talk about one question:

_ Switch_data in head_common.s:

. Long init_thread_union + thread_start_sp

Init_thread_union cannot be redirected using ctags, and there is no init_thread_union in the Link script. Using grep search, we found that this is the name of the federated variable in arch/Unicore/kernel/init_task.c3 (no jump, there is no link to the script, that is, the Assembly file of the file is not like the C file, the header file needs to be included, and the extern external declaration is added ). So we added a constant value to the variable name of the consortium in C in head_common.s. This is because the name of a consortium variable in C is treated as a label, just like the label in the Assembly file. Therefore, a constant value can be added to the name of a consortium variable in C. I wrote a very small test (the program has no meaning, in the form !!!) :

Sramboot. s file:

1 # include "regdef. H"

2. Set noreorder

3. Text

4. globl start

5 start:

6. Set noat

7 la at, Value

8 LW A1, 4 ()

9 Jal my_add

10 NOP

11 add A2, A0, A1

12 SW A2, 8 ()

 

Call. s file:

1 # include "regdef. H"

2 /*

3 extern Union thread_union init_thread_union;

4 */

5. Set noreorder

6. Text

7. globl my_add, Value

8 my_add:

9 Add A1, A1, 3

10 Jal start_kernel

11 J Ra

12 add A1, A1, 4

13. Data

14 value:

15. Word 10, 20, 0

16. Long init_thread_union + 8

 

Union. c file:

1 # DEFINE _ init_task_data _ attribute _ (_ Section _ (". Data. init_task ")))

2 struct thread_info

3 {

4 int ID;

5 Int time;

6 };

7 Union thread_union

8 {

9 struct thread_info;

10 unsigned long stack [16];

11 };

12

13 Union thread_union init_thread_union _ init_task_data =

14 {

15 {. ID = 9,. Time = 8 ,},

16 };

17 void start_kernel (void)

18 {

19 while (1 );

20}

 

Makefile

1 cross_compile = mipsel-Linux-

2 cc = $ (cross_compile) GCC

3 As = $ (cross_compile)

4 LD = $ (cross_compile) LD

5 objdump = $ (cross_compile) objdump

6 objcopy = $ (cross_compile) objcopy

7 textbase = #-ttext

8 Database =

9 ldflags = $ (textbase) $ (database)-t my. LD

10

11 test. Bin: test.exe

12 $ (objcopy)-O binary test.exe test. Bin

13

14 test.exe: Union. O call. O sramboot. o

15 $ (LD) sramboot. O call. o union. o $ (ldflags)-O test.exe

16 $ (objdump)-D test.exe> test. Dump

17

18 sramboot. O: sramboot. s

19 $ (CC)-E sramboot. S-O sramboot. s

20 $ (AS) sramboot. S-O sramboot. o

21 @ rM sramboot. s

22

23 call. O: Call. s

24 $ (CC)-E call. S-O call. s

25 $ (AS) Call. S-O call. o

26 @ rM call. s

27 Union. O: Union. c

28 $ (CC)-C Union. C-o union. o

29 clean:

30 @ rM *. O *. EXE *. Bin *. Dump

 

My. LD link script file:

1output_format ("elf32-tradlittlemips", "elf32-tradbigmips", "elf32-tradlittlemips ")

2 output_arch (MIPs)

3 entry (start)

4 sections

5 {

6. = 0;

7. init:

8 {

9 sramboot. o

10}

11

12. Text:

13 {

14 * (. Text)

15}

16. Data:

17 {

18. = align (32 );

19 * (. Data. init_task );

20. = align (16 );

21 * (. Data)

22}

23. BSS:

24 {

25 * (. BSS)

26}

27}

 

Test. Sh Native Binary disassembly script:

1 #! /Bin/sh

2 mipsel-Linux-objdump-D-m mips-B Binary-el-m no-aliases-z test. Bin> my. Dump

 

Run the test. dump file generated by make:

1

2 test.exe: File Format elf32-tradlittlemips

3

4 disassembly of section. init:

5

6 00000000 <Start>:

7 0: 3c010000 lui at, 0x0

8 4: 242100c0 addiu at, at, 192

9 8: 8c250004 LW A1, 4 ()

10 C: 0c000010 Jal 40 <my_add>

11 10: 00000000 NOP

12 14: 00853020 add A2, A0, A1

13 18: ac260008 SW A2, 8 ()

14 1c: 00000000 NOP

15 20: 90000072 lbu zero, 114 (zero)

16...

17 disassembly of section. Text:

18

19 00000040 <my_add>:

20 40: 20a50003 addi A1, A1, 3

21 44: 0c000014 Jal 50 <start_kernel>

22 48: 03e00008 Jr Ra

23 4c: 20a50004 addi A1, A1, 4

24

25 00000050 <start_kernel>:

26 50: 27bdfff8 addiu sp, SP,-8

27 54: afbe0000 SW S8, 0 (SP)

28 58: 03a0f021 move S8, SP

29 5C: 1000 ffff B 5C <start_kernel + 0xc>

30 60: 00000000 NOP

31...

32 disassembly of section. Data:

33

34 00000070 <init_thread_union-0x10>:

35...

36

37 00000080 <init_thread_union>:

38 80: 00000009 jalr zero, zero

39 84: 00000008 Jr zero

40 88: 00000000 NOP

41...

42

43 1000000c0 <value>:

44 C0: 0000000a 0xa

45 C4: 00000014 0x14

46 C8: 00000000 NOP

47 CC: 00000088 0x88

48 disassembly of section. PDR:

49

50 00000000 <. PDR>:

51 0: 00000050 0x50

52 4: 40000000 mfc0 zero, $0

53 8: fffffff8 0xfffffff8

54...

55 14: 00000008 Jr zero

56 18: 0000001e 0x1e

57 1c: 0000001f 0x1f

58 disassembly of section. Comment:

59

60 00000000 <. Comment>:

61 0: 43434700 C0 0x1434700

62 4: 4728203a C1 0x128203a

63 8: 2029554e addi T1, AT, 21838

64 C: 2e312e34 sltiu S1, S1, 11828

65 10: Address 0x0000000000000010 is out of bounds.

 

Execute the my. dump file generated by test. sh:

1

2 test. Bin: File Format binary

3

4 disassembly of section. Data:

5

6 0000000000000000 <. Data>:

7 0: 3c010000 lui at, 0x0

8 4: 242100c0 addiu at, at, 192

9 8: 8c250004 LW A1, 4 ()

10 C: 0c000010 Jal 0x40

11 10: 00000000 sll zero, zero, 0x0

12 14: 00853020 add A2, A0, A1

13 18: ac260008 SW A2, 8 ()

14 1c: 00000000 sll zero, zero, 0x0

15 20: 90000072 lbu zero, 114 (zero)

16 24: 00000000 sll zero, zero, 0x0

17 28: 00000000 sll zero, zero, 0x0

18 2C: 00000000 sll zero, zero, 0x0

19 30: 00000000 sll zero, zero, 0x0

20 34: 00000000 sll zero, zero, 0x0

21 38: 00000000 sll zero, zero, 0x0

22 3C: 00000000 sll zero, zero, 0x0

23 40: 20a50003 addi A1, A1, 3

24 44: 0c000014 Jal 0x50

25 48: 03e00008 Jr Ra

26 4c: 20a50004 addi A1, A1, 4

27 50: 27bdfff8 addiu sp, SP,-8

28 54: afbe0000 SW S8, 0 (SP)

29 58: 03a0f021 Addu S8, SP, zero

30 5C: 1000 FFFF beqz zero, 0x5c

31 60: 00000000 sll zero, zero, 0x0

32 64: 00000000 sll zero, zero, 0x0

33 68: 00000000 sll zero, zero, 0x0

34 6C: 00000000 sll zero, zero, 0x0

35 70: 00000000 sll zero, zero, 0x0

36 74: 00000000 sll zero, zero, 0x0

37 78: 00000000 sll zero, zero, 0x0

38 7c: 00000000 sll zero, zero, 0x0

39 80: 00000009 jalr zero, zero

40 84: 00000008 Jr zero

41 88: 00000000 sll zero, zero, 0x0

42 8c: 00000000 sll zero, zero, 0x0

43 90: 00000000 sll zero, zero, 0x0

44 94: 00000000 sll zero, zero, 0x0

45 98: 00000000 sll zero, zero, 0x0

46 9C: 00000000 sll zero, zero, 0x0

47 A0: 00000000 sll zero, zero, 0x0

48 A4: 00000000 sll zero, zero, 0x0

49 A8: 00000000 sll zero, zero, 0x0

50 AC: 00000000 sll zero, zero, 0x0

51 B0: 00000000 sll zero, zero, 0x0

52 B4: 00000000 sll zero, zero, 0x0

53 B8: 00000000 sll zero, zero, 0x0

54 BC: 00000000 sll zero, zero, 0x0

55 C0: 0000000a 0xa

56 C4: 00000014 dsllv zero, zero, zero

57 C8: 00000000 sll zero, zero, 0x0

58 CC: 00000088 0x88

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.