QUESTION
x86 relocation은 어떤 조건에서 실행되고, CAR 해제 호출은 어느 경로에 있는가?
board_init_f의 x86 initcall은 copy_uboot_to_ram → do_elf_reloc_fixups → clear_bss → jump_to_copy 순서다. GD_FLG_SKIP_RELOC이면 copy·fixup·jump는 조기 반환하며, non-EFI-app x86 clear_bss도 조기 반환한다.
정상 jump에서는 새 gd를 설정한 뒤 arch trampoline으로 간다. 32비트는 새 ESP 후 car_uninit을 호출하고, 64비트는 새 RSP 뒤 바로 board_init_f_r을 호출한다. 32비트의 car_uninit도 기본 weak 구현은 단순 return이다. 이 글의 x86 clear_bss 구현은 !CONFIG_EFI_APP 범위다.
STRUCTURE
객체와 주소가 놓이는 구조
source image, destination, relocation entry, BSS, gd와 stack의 역할을 놓았다. 블록의 번호는 물리 주소 배치나 항상 실행되는 순서가 아니다.
CALL PATH
skip 조건과 두 trampoline
GD_FLG_SKIP_RELOC 없음
CONFIG_TEXT_BASE가 있으면 offset 계산; new gd 복사.- copy → ELF fixup → BSS clear →
jump_to_copy. arch_setup_gd뒤 32/64비트 trampoline 선택.
copy는 __text_start..__data_end; 표시한 BSS clear는 non-EFI-app x86 helper다.
GD_FLG_SKIP_RELOC 있음
setup_reloc의 offset 계산 건너뜀; new gd 복사는 수행.- copy·ELF fixup·jump가 즉시 0 반환; non-EFI-app x86 BSS helper도 동일.
- 후속
board_init_f반환 처리는 빌드 경로에 따라 다름.
64비트 start64.S는 반환 뒤 board_init_f_r을 호출한다. 32비트 일반 경로에는 NOTREACHED hang가 있다.
정상 jump의 32비트 trampoline은 ESP → car_uninit → board_init_f_r, 64비트는 RSP → board_init_f_r이다. jump_to_copy · 32비트 · 64비트.
STATE LEDGER
실행 조건과 결과
행 02–05의 함수 호출은 initcall 목록에 있지만, relocation skip flag가 있으면 내부 작업은 수행되지 않는다.
| # | 단계·조건 | 실제 작업 | 남는 상태 | 근거 |
|---|---|---|---|---|
| 01 | setup_reloc | skip 아님 + CONFIG_TEXT_BASE에서 offset 계산; 항상 gd->new_gd 복사 | new gd와 조건부 reloc_off | setup |
| 02 | copy · skip 아님 | __text_start..__data_end를 gd->relocaddr에 복사 | destination text/data bytes | copy |
| 03 | ELF fixup · skip 아님 | 64비트 Elf64_Rela 또는 32비트 Elf32_Rel | 지원되는 relative target 갱신 | fixup |
| 04 | BSS · skip 아님, non-EFI app | __bss_start + reloc_off에서 길이만큼 zero | 새 image의 BSS 0 | clear |
| 05 | jump_to_copy · skip 아님 | arch_setup_gd, arch trampoline 호출 | 새 gd·stack 인자로 후반부 진입 | jump |
| 06a | 32비트 trampoline | ESP 설정 → car_uninit 호출 → board_init_f_r | CAR 함수는 weak 기본 no-op 가능 | start.S |
| 06b | 64비트 trampoline | RSP 설정 → board_init_f_r | 이 경로에 car_uninit 호출 없음 | start64.S |
skip 상태에서 64비트 _start는 board_init_f 반환 뒤 board_init_f_r을 호출한다. 32비트 일반 build의 NOTREACHED hang는 별도 조건이다.
VISUAL WALKTHROUGH
주소 역할과 trampoline 분기
아래 그림은 물리 주소의 높낮이가 아니라 producer와 consumer를 표시한다. 실제 수치는 보드의 bdinfo와 ELF symbol로 확인한다.
source image와 old CAR를 DRAM bank의 낮은 주소 영역으로 그리지 않는다. 둘의 위치와 수명은 플랫폼 경로에 달려 있다.
copy source는 __text_start; old gd는 setup_reloc에서 새 gd로 복사한다.
gd->relocaddr · BSStext/data bytes를 복사하고, fixup 대상 word를 갱신하며 non-EFI-app x86 BSS는 별도 clear.
start_addr_sparch_setup_gd 뒤 trampoline이 새 stack pointer를 설정한다.
car_uninitESP 전환 뒤 호출한다. weak 기본 함수는 return만 하므로 실제 CAR 해제 여부는 구현에 따른다.
이 순서는 GD_FLG_SKIP_RELOC이 없는 정상 relocation 경로다. 그림의 BSS clear는 non-EFI-app x86 helper를 기준으로 한다.
32비트 · start.S
arch_setup_gd(new_gd); stack top을 EAX로 전달.movl %eax, %esp.car_uninit호출 (weak 기본 no-op 가능).board_init_f_r호출.
새 stack을 선택한 뒤에만 CAR 관련 함수를 호출한다.
64비트 · start64.S
arch_setup_gd(new_gd); new gd·stack 인자를 전달.movq %rsi, %rsp.board_init_f_r호출.
이 trampoline에는 car_uninit 호출이 없다.
함수별로 어떤 값을 생산·검사하는지 묶었다. Elf32_Rel과 Elf64_Rela는 구분한다.
setup_reloc는 이미 지정된 gd->relocaddr를 이용해 offset을 조건부 계산하고 gd를 복사한다.
copy 길이는 __data_end - __text_start; non-EFI-app BSS 길이는 __bss_end - __bss_start.
32비트 Rel과 64비트 Rela의 형식과 갱신식을 나눠 본다.
jump_to_copy가 skip이 아닐 때 해당 trampoline에 넘긴다.
UPSTREAM SOURCE
원본 코드
아래 코드는 U-Boot v2026.07의 arch/x86/cpu/start.S에서 173-224줄을 그대로 가져온 것이다.
173 post_code(POST_START_DONE)
174 xorl %eax, %eax
175
176 /* Enter, U-Boot! */
177 call board_init_f
178
179 /* indicate (lack of) progress */
180 movw $0x85, %ax
181 jmp die
182
183.globl board_init_f_r_trampoline
184.type board_init_f_r_trampoline, @function
185board_init_f_r_trampoline:
186 /*
187 * SDRAM has been initialised, U-Boot code has been copied into
188 * RAM, BSS has been cleared and relocation adjustments have been
189 * made. It is now time to jump into the in-RAM copy of U-Boot
190 *
191 * %eax = Address of top of new stack
192 */
193
194 /* Stack grows down from top of SDRAM */
195 movl %eax, %esp
196
197 /* See if we need to disable CAR */
198 call car_uninit
199
200 /* Re-enter U-Boot by calling board_init_f_r() */
201 call board_init_f_r
202
203#ifdef CONFIG_TPL
204.globl jump_to_spl
205.type jump_to_spl, @function
206jump_to_spl:
207 /* Reset stack to the top of CAR space */
208 movl $(CONFIG_SYS_CAR_ADDR + CONFIG_SYS_CAR_SIZE - 4), %esp
209#ifdef CONFIG_DCACHE_RAM_MRC_VAR_SIZE
210 subl $CONFIG_DCACHE_RAM_MRC_VAR_SIZE, %esp
211#endif
212
213 jmp *%eax
214#endif
215
216die:
217 hlt
218 jmp die
219 hlt
220
221WEAK(car_uninit)
222 ret
223ENDPROC(car_uninit)
224
READING CONTRACT
발췌 범위와 읽기 기준
원문 173-224줄에는 앞뒤 문맥이 포함됩니다. 기준 구문 board_init_f_r_trampoline:은 L185에 있습니다. 발췌 첫 줄을 함수 정의의 시작으로 해석하지 않습니다.
x86/ARC 조건부 initcall은 copy·fixup 뒤 BSS clear와 jump를 배치한다. skip flag가 있으면 실제 작업을 건너뛰며, CAR 관련 호출은 32비트 trampoline에만 있다.
board_init_f_r_trampoline:복사와 fixup 뒤 새 ESP로 전환하는 32비트 trampoline이다. 이 호출 자체가 RAM 코드로의 최종 이동은 아니다. board_init_f_r()가 뒤에서 board_init_r + gd->reloc_off를 호출한다.
movl %eax, %espcaller가 EAX로 넘긴 DRAM stack top을 활성 stack으로 만든다. 이후 old CAR stack은 더 이상 사용하지 않는다.
call car_uninit32비트에서 ESP를 새 stack으로 바꾼 뒤 호출한다. 고정 소스의 weak 기본 구현은 return만 하므로 실제 CAR 종료는 플랫폼 override에 따른다.
call board_init_f_r새 stack에서 board_init_f_r()로 간다. 이 함수는 serial/log 준비 flag와 조건부 timer 포인터를 정리한 뒤 board_init_r + gd->reloc_off로 RAM 실행을 넘긴다.
RELATED SOURCE
함께 읽어야 하는 원본 코드
첫 코드 조각만으로 동작이 완성되지 않는 경우 호출 매크로, 객체 정의와 실제 실행 목록을 같은 페이지에 묶었다. 각 조각은 같은 기준 commit에서 가져왔다.
01. U-Boot image byte를 gd->relocaddr로 복사
arch/x86/lib/relocate.c 19-59줄이다.
19#include <asm/global_data.h>
20#include <asm/u-boot-x86.h>
21#include <asm/sections.h>
22#include <elf.h>
23
24DECLARE_GLOBAL_DATA_PTR;
25
26int copy_uboot_to_ram(void)
27{
28 size_t len = (uintptr_t)__data_end - (uintptr_t)__text_start;
29
30 if (gd->flags & GD_FLG_SKIP_RELOC)
31 return 0;
32 memcpy((void *)gd->relocaddr, (void *)__text_start, len);
33
34 return 0;
35}
36
37#ifndef CONFIG_EFI_APP
38int clear_bss(void)
39{
40 ulong dst_addr = (ulong)__bss_start + gd->reloc_off;
41 size_t len = (uintptr_t)__bss_end - (uintptr_t)__bss_start;
42
43 if (gd->flags & GD_FLG_SKIP_RELOC)
44 return 0;
45 memset((void *)dst_addr, 0x00, len);
46
47 return 0;
48}
49#endif
50
51#if CONFIG_IS_ENABLED(X86_64)
52static void do_elf_reloc_fixups64(unsigned int text_base, uintptr_t size,
53 Elf64_Rela *re_src, Elf64_Rela *re_end)
54{
55 Elf64_Addr *offset_ptr_rom, *last_offset = NULL;
56 Elf64_Addr *offset_ptr_ram;
57
58 do {
59 unsigned long long type = ELF64_R_TYPE(re_src->r_info);
주요 구문 해설
unsigned long long type = ELF64_R_TYPE(re_src->r_info);ELF64 r_info에서 relocation type을 분리한다. 이 구현은 R_X86_64_RELATIVE만 지원하므로 다른 type은 같은 식으로 고치지 않는다.
02. x86-64 ELF relocation entry를 새 image에 적용
arch/x86/lib/relocate.c 47-120줄이다.
47 return 0;
48}
49#endif
50
51#if CONFIG_IS_ENABLED(X86_64)
52static void do_elf_reloc_fixups64(unsigned int text_base, uintptr_t size,
53 Elf64_Rela *re_src, Elf64_Rela *re_end)
54{
55 Elf64_Addr *offset_ptr_rom, *last_offset = NULL;
56 Elf64_Addr *offset_ptr_ram;
57
58 do {
59 unsigned long long type = ELF64_R_TYPE(re_src->r_info);
60
61 if (type != R_X86_64_RELATIVE) {
62 printf("%s: unsupported relocation type 0x%llx "
63 "at %p, ", __func__, type, re_src);
64 printf("offset = 0x%llx\n", re_src->r_offset);
65 continue;
66 }
67
68 /* Get the location from the relocation entry */
69 offset_ptr_rom = (Elf64_Addr *)(uintptr_t)re_src->r_offset;
70
71 /* Check that the location of the relocation is in .text */
72 if (offset_ptr_rom >= (Elf64_Addr *)(uintptr_t)text_base &&
73 offset_ptr_rom > last_offset) {
74 /* Switch to the in-RAM version */
75 offset_ptr_ram = (Elf64_Addr *)((ulong)offset_ptr_rom +
76 gd->reloc_off);
77
78 /* Check that the target points into .text */
79 if (*offset_ptr_ram >= text_base &&
80 *offset_ptr_ram <= text_base + size) {
81 *offset_ptr_ram = gd->reloc_off +
82 re_src->r_addend;
83 } else {
84 debug(" %p: %lx: rom reloc %lx, ram %p, value %lx, limit %lX\n",
85 re_src, (ulong)re_src->r_info,
86 (ulong)re_src->r_offset, offset_ptr_ram,
87 (ulong)*offset_ptr_ram, text_base + size);
88 }
89 } else {
90 debug(" %p: %lx: rom reloc %lx, last %p\n", re_src,
91 (ulong)re_src->r_info, (ulong)re_src->r_offset,
92 last_offset);
93 }
94 last_offset = offset_ptr_rom;
95
96 } while (++re_src < re_end);
97}
98#else
99static void do_elf_reloc_fixups32(unsigned int text_base, uintptr_t size,
100 Elf32_Rel *re_src, Elf32_Rel *re_end)
101{
102 Elf32_Addr *offset_ptr_rom, *last_offset = NULL;
103 Elf32_Addr *offset_ptr_ram;
104
105 do {
106 unsigned int type = ELF32_R_TYPE(re_src->r_info);
107
108 if (type != R_386_RELATIVE) {
109 printf("%s: unsupported relocation type 0x%x "
110 "at %p, ", __func__, type, re_src);
111 printf("offset = 0x%x\n", re_src->r_offset);
112 continue;
113 }
114
115 /* Get the location from the relocation entry */
116 offset_ptr_rom = (Elf32_Addr *)(uintptr_t)re_src->r_offset;
117
118 /* Check that the location of the relocation is in .text */
119 if (offset_ptr_rom >= (Elf32_Addr *)(uintptr_t)text_base &&
120 offset_ptr_rom > last_offset) {
주요 구문 해설
unsigned long long type = ELF64_R_TYPE(re_src->r_info);ELF64 r_info에서 relocation type을 분리한다. 이 구현은 R_X86_64_RELATIVE만 지원하므로 다른 type은 같은 식으로 고치지 않는다.
offset_ptr_ram = (Elf64_Addr *)((ulong)offset_ptr_rom +link-time patch 주소에 gd->reloc_off를 더해 destination image에서 실제로 쓸 8-byte word 위치를 만든다.
*offset_ptr_ram = gd->reloc_off +R_X86_64_RELATIVE addend는 link-time address이므로 gd->reloc_off + r_addend 식으로 destination word를 쓴다. 앞선 검사는 기존 word의 값에 적용되며 addend가 별도로 검증됐다는 뜻은 아니다.
03. x86 relocation initcall의 실제 순서
common/board_f.c 990-1051줄이다.
990 CONFIG_IS_ENABLED(OF_INITIAL_DTB_READONLY)
991 INITCALL(reloc_fdt);
992 INITCALL(fix_fdt);
993#endif
994 INITCALL(reserve_bootstage);
995 INITCALL(reserve_bloblist);
996 INITCALL(reserve_arch);
997 INITCALL(reserve_stacks);
998 INITCALL(dram_init_banksize);
999 INITCALL(show_dram_config);
1000 WATCHDOG_RESET();
1001 INITCALL(setup_bdinfo);
1002 INITCALL(display_new_sp);
1003 WATCHDOG_RESET();
1004#if !CONFIG_IS_ENABLED(OF_BOARD_FIXUP) || \
1005 !CONFIG_IS_ENABLED(INITIAL_DTB_READONLY)
1006 INITCALL(reloc_fdt);
1007#endif
1008 INITCALL(reloc_bootstage);
1009 INITCALL(reloc_bloblist);
1010 INITCALL(setup_reloc);
1011#if CONFIG_IS_ENABLED(X86) || CONFIG_IS_ENABLED(ARC)
1012 INITCALL(copy_uboot_to_ram);
1013 INITCALL(do_elf_reloc_fixups);
1014#endif
1015 INITCALL(clear_bss);
1016 /*
1017 * Deregister all cyclic functions before relocation, so that
1018 * gd->cyclic_list does not contain any references to pre-relocation
1019 * devices. Drivers will register their cyclic functions anew when the
1020 * devices are probed again.
1021 *
1022 * This should happen as late as possible so that the window where a
1023 * watchdog device is not serviced is as small as possible.
1024 */
1025 INITCALL(cyclic_unregister_all);
1026#if !CONFIG_IS_ENABLED(ARM) && !CONFIG_IS_ENABLED(SANDBOX)
1027 INITCALL(jump_to_copy);
1028#endif
1029}
1030
1031void board_init_f(ulong boot_flags)
1032{
1033 struct board_f boardf;
1034
1035 gd->flags = boot_flags;
1036 gd->flags &= ~GD_FLG_HAVE_CONSOLE;
1037 gd->boardf = &boardf;
1038
1039 initcall_run_f();
1040
1041#if !defined(CONFIG_ARM) && !defined(CONFIG_SANDBOX) && \
1042 !defined(CONFIG_EFI_APP) && !CONFIG_IS_ENABLED(X86_64) && \
1043 !defined(CONFIG_ARC)
1044 /* NOTREACHED - jump_to_copy() does not return */
1045 hang();
1046#endif
1047}
1048
1049#if defined(CONFIG_X86) || defined(CONFIG_ARC)
1050/*
1051 * For now this code is only used on x86.
04. 64-bit 새 stack과 gd를 받는 trampoline
arch/x86/cpu/start64.S 26-49줄이다.
26 call board_init_f
27 call board_init_f_r
28
29 /* Should not return here */
30 jmp .
31
32.globl board_init_f_r_trampoline64
33.type board_init_f_r_trampoline64, @function
34board_init_f_r_trampoline64:
35 /*
36 * SDRAM has been initialised, U-Boot code has been copied into
37 * RAM, BSS has been cleared and relocation adjustments have been
38 * made. It is now time to jump into the in-RAM copy of U-Boot
39 *
40 * %eax = Address of top of new stack
41 */
42
43 /* Stack grows down from top of SDRAM */
44 movq %rsi, %rsp
45
46 /* New gd is in rdi */
47
48 /* Re-enter U-Boot by calling board_init_f_r() */
49 call board_init_f_r
주요 구문 해설
call board_init_f_r64비트 board_init_f가 반환한 경우의 후속 호출이다. skip-reloc 경로에서는 trampoline을 거치지 않을 수 있다.
call board_init_f_r새 stack에서 board_init_f_r()로 간다. 이 함수는 serial/log 준비 flag와 조건부 timer 포인터를 정리한 뒤 board_init_r + gd->reloc_off로 RAM 실행을 넘긴다.
05. 새 gd와 stack을 architecture trampoline으로 넘기는 jump_to_copy
common/board_f.c 744-785줄이다.
744{
745 return board_fix_fdt((void *)gd->fdt_blob);
746}
747#endif
748
749/* ARM calls relocate_code from its crt0.S */
750#if !defined(CONFIG_ARM) && !defined(CONFIG_SANDBOX)
751
752static int jump_to_copy(void)
753{
754 if (gd->flags & GD_FLG_SKIP_RELOC)
755 return 0;
756 /*
757 * x86 is special, but in a nice way. It uses a trampoline which
758 * enables the dcache if possible.
759 *
760 * For now, other archs use relocate_code(), which is implemented
761 * similarly for all archs. When we do generic relocation, hopefully
762 * we can make all archs enable the dcache prior to relocation.
763 */
764#if defined(CONFIG_X86) || defined(CONFIG_ARC)
765 /*
766 * SDRAM and console are now initialised. The final stack can now
767 * be setup in SDRAM. Code execution will continue in Flash, but
768 * with the stack in SDRAM and Global Data in temporary memory
769 * (CPU cache)
770 */
771 arch_setup_gd(gd->new_gd);
772# if CONFIG_IS_ENABLED(X86_64)
773 board_init_f_r_trampoline64(gd->new_gd, gd->start_addr_sp);
774# else
775 board_init_f_r_trampoline(gd->start_addr_sp);
776# endif
777#else
778 relocate_code(gd->start_addr_sp, gd->new_gd, gd->relocaddr);
779#endif
780
781 return 0;
782}
783#endif
784
785/* Record the board_init_f() bootstage (after arch_cpu_init()) */
주요 구문 해설
static int jump_to_copy(void)skip flag에서는 0으로 돌아온다. 정상 x86 경로에서는 새 gd를 지정한 뒤 32/64비트 trampoline을 호출한다.
arch_setup_gd(gd->new_gd);trampoline에 넘기기 전 새 gd를 architecture GD access에 설정한다. 구체적 register 방식은 선택된 x86 빌드의 구현을 확인한다.
board_init_f_r_trampoline64(gd->new_gd, gd->start_addr_sp);RDI=new gd, RSI=new stack을 넘긴다. 64비트 trampoline은 RSP를 바꾼 뒤 board_init_f_r를 직접 호출하며 CAR hook은 없다.
DETAILS
조건부 initcall을 읽는 기준
skip flag를 먼저 확인한다
setup_reloc은 skip이 아니고 CONFIG_TEXT_BASE가 있을 때 gd->reloc_off를 계산한다. 새 gd 복사는 skip 여부와 무관하게 수행한다.
copy, ELF fixup, non-EFI-app x86 BSS clear, jump_to_copy는 각각 GD_FLG_SKIP_RELOC 검사 뒤 작업 또는 조기 0 반환을 택한다.
copy와 BSS의 범위를 분리한다
copy_uboot_to_ram은 __data_end - __text_start byte를 gd->relocaddr로 복사한다. BSS는 복사 대상에 포함하지 않고, __bss_start + gd->reloc_off에서 별도 0으로 만든다.
두 범위와 destination reservation을 linker map에서 대조한다.
relocation table 형식도 arch 폭에 따른다
64비트는 Elf64_Rela의 R_X86_64_RELATIVE, 32비트는 Elf32_Rel의 R_386_RELATIVE 경로를 사용한다. 두 함수 모두 patch 위치의 하한·이전 위치보다 큰지와 현재 destination word의 값 범위를 검사한다.
patch 위치의 상한이나 word 전체 크기는 여기서 검사하지 않으며, 64비트 addend 자체의 범위를 검사하는 조건도 아니다. 따라서 이 코드를 임의 ELF 입력의 완전한 검증기로 해석하지 않는다. unsupported type은 로그를 남기고 넘어가므로 출력과 갱신되지 않은 target을 함께 확인한다.
jump는 새 gd를 먼저 지정한다
skip이 아니면 jump_to_copy가 arch_setup_gd(gd->new_gd) 뒤 arch trampoline을 부른다. 32비트에는 새 stack top, 64비트에는 new gd와 stack을 전달한다.
trampoline 직전의 gd와 gd->start_addr_sp를 기록한다.
CAR 호출의 적용 범위를 제한한다
32비트 trampoline은 새 ESP 설정 후 car_uninit을 호출한다. 실제 종료 동작은 플랫폼 구현에 따르며, 고정 start.S의 weak 기본 구현은 바로 반환한다.
64비트 trampoline에는 그 호출이 없다. board_init_f_r 진입 검사는 선택된 경로의 SP와 gd를 기준으로 한다.
IMPLEMENTATION NOTES
소스에서 갈라지는 경계
initcall 목록의 순서와 guard는 별개다
common/board_f.c는 x86/ARC에서 copy와 fixup을, 그 뒤 BSS clear와 jump를 배치한다. 목록의 존재가 실제 copy를 보장하지 않는다. 각 함수가 skip flag를 다시 검사한다.
setup_reloc는 gd를 복사한다
skip이 아니고 CONFIG_TEXT_BASE가 있으면 이를 기준으로 reloc_off를 계산한다. 그 뒤 memcpy(gd->new_gd, gd, sizeof(gd_t))를 실행한다. skip이면 offset 계산을 건너뛰지만 gd 복사는 남는다.
64비트 Rela와 32비트 Rel은 값 계산이 다르다
64비트 helper는 지원되는 relative entry의 addend와 gd->reloc_off로 destination word를 갱신한다. 32비트 helper는 destination word의 기존 값에 reloc_off를 더한다.
따라서 32비트 entry를 64비트 addend 도식으로 읽지 않는다. relocate.c.
skip-reloc의 반환 경로도 arch별로 본다
jump_to_copy는 skip flag에서 0을 반환한다. 64비트 _start는 이 경우 board_init_f 반환 뒤 board_init_f_r를 직접 호출할 수 있다.
반면 일반 32비트 board_init_f 끝에는 NOTREACHED hang()가 있다. skip flag를 정상 32비트 trampoline의 우회 경로로 그리지 않는다. jump · tail · 64비트 start.
최종 RAM 진입까지 나눠 확인한다
정상 relocation에서는 destination byte, relative target word, BSS zero, new gd·SP와 board_init_f_r 진입을 확인한다. 32비트에만 car_uninit 호출 여부를 더한다.
skip 경로에서는 copy/fixup/clear 결과를 강제하지 않고 gd->flags와 실제 continuation을 먼저 기록한다.
board_init_f_r()는 32비트에서 init_cache_f_r을 호출하고, serial/log ready flag 및 조건부 timer 포인터를 정리한 다음 (board_init_r + gd->reloc_off)(gd, gd->relocaddr)로 최종 RAM 진입한다. trampoline의 새 SP와 이 최종 PC 전환을 같은 사건으로 세지 않는다. 후반부 실제 호출.
OBJECT LIFETIME
relocation 입력과 결과
| 대상 | 생산/전환 시점 | 소스가 보장하는 상태 | 근거 |
|---|---|---|---|
old gd / gd->new_gd | setup_reloc | skip이어도 gd 복사; 정상 jump 직전 새 gd 설정 | board_f.c |
| destination image bytes | skip 아닐 때 copy_uboot_to_ram | __text_start..__data_end 복사 | copy |
| relative fixup target | skip 아닐 때 do_elf_reloc_fixups | 32비트 Rel / 64비트 Rela 분리 | fixup |
| new BSS | skip 아닐 때 clear_bss | relocated BSS 범위 zero | clear |
| new stack / CAR hook | trampoline의 ESP 또는 RSP 설정 | 32비트만 car_uninit 호출; 기본 weak no-op | 32비트 / 64비트 |
FAILURE PATH
조건을 잘못 읽을 때의 오판
| # | 조건 | 소스상 결과 / 관찰점 | 먼저 볼 곳 |
|---|---|---|---|
| 01 | GD_FLG_SKIP_RELOC | copy·fixup·clear·jump 본체를 건너뜀 | 각 함수의 guard와 32/64비트 continuation |
| 02 | relocation entry 없음 | skip이 아니면 panic("No relocation data") | __rel_dyn_start/end |
| 03 | unsupported relocation type | 로그를 남기고 해당 entry는 갱신하지 않음 | 32/64비트 entry type |
| 04 | 32비트 CAR hook | ESP 설정 뒤 호출; weak 기본은 return | 플랫폼의 car_uninit override |
| 05 | 64비트 trampoline | RSP 뒤 바로 board_init_f_r | start64.S; CAR 호출을 찾지 않음 |
relocation이 없었던 상태와 실패를 구분한다. skip flag, entry 범위, arch trampoline의 실제 명령을 함께 확인한다.
EVIDENCE
빌드·런타임에서 확인할 증거
대상 image의 CONFIG_X86_64, CONFIG_EFI_APP와 GD_FLG_SKIP_RELOC 값을 함께 기록한다.
| # | 목적 | 명령 또는 계측 | 판정 기준 |
|---|---|---|---|
| 01 | 조건 분기 | 대상 .config와 gd->flags | 64/32비트, EFI app, skip flag를 고정한다. |
| 02 | relocation 형식 | readelf -rW u-boot | 32비트 Rel / 64비트 Rela와 지원 type을 대조한다. |
| 03 | symbol/명령 | nm -n u-boot; objdump -drS u-boot | 선택된 trampoline, copy·fixup·BSS의 주소와 guard를 확인한다. |
| 04 | 주소 배치 | bdinfo와 linker map | relocaddr, new gd, stack, copy/BSS 범위를 대조한다. |
| 05 | 전환 계측 | GDB: PC·SP·gd·gd->flags | 32비트 CAR hook 호출 여부와 64비트 직접 board_init_f_r을 구분한다. |
LAB
직접 확인할 실험
- 01skip flag와 실제 copy 비교
같은 image에서
gd->flags와copy_uboot_to_ram진입·반환을 기록하고 destination byte 변화 여부를 비교한다. - 0232비트 Rel / 64비트 Rela target 계산
readelf -rW의 offset/type을 destination word와 대조한다. 64비트 addend 사용과 32비트 기존 word 가산을 혼동하지 않는다. - 03trampoline 분기 계측
32비트면 ESP 변경 후
car_uninit호출과 override 유무를, 64비트면 RSP 변경 뒤board_init_f_r직접 호출을 확인한다. - 04후반부 진입 상태 확인
board_init_f_r에서 PC·SP·gd, BSS zero와 선택된 relocation target을 한 번에 기록한다. skip 경로는 적용되지 않은 작업을 성공 증거로 세지 않는다.
PRIMARY REFERENCES