Skip to contentSkip to conversation

Detecting endianness in scripts/recordmcount.pl?

PATCH Paul Mundt 3 participants 6 messages Nov 26, 2008 Kbuild

lore
Share & tools
lore

Paul Mundt

author
#0 lore raw

Presently there doesn't seem to be any way to determine whether the target is big or little endian, and it is assumed that the compiler will do the right thing by default. Unfortunately this can not be assumed, and mismatches ensue, resulting in the linker bailing out.

The only obvious solution I saw was to pass in KBUILD_CFLAGS and ld_flags along with $(CC) and $(LD) to the script, and killing off the hardcoded flags. This at least gets things building, but that still leaves objcopy and objdump as the odd ones out. On the other hand, the format can be figured out by objdumping the object and reading in the file format line, but people obviously do not have consistent naming for these, and a double-pass would be needed -- once for establishing little or big, followed by figuring out which set of regexes to use.

The CONFIG_64BIT test could likewise be adopted for testing endianness, but not all architectures have config options for endian selections. Likewise, a simple test program to check if __LITTLE_ENDIAN is set or not won't work without having knowledge of KBUILD_CFLAGS, in which case it seems saner just to pass it off as a starting point.

Any better ideas?

---

scripts/Makefile.build +2 -1
index 7a17677..de2cc42 100644
@@ -201,7 +201,8 @@ endif
 ifdef CONFIG_FTRACE_MCOUNT_RECORD
 cmd_record_mcount = perl $(srctree)/scripts/recordmcount.pl "$(ARCH)" \
 	"$(if $(CONFIG_64BIT),64,32)" \
-	"$(OBJDUMP)" "$(OBJCOPY)" "$(CC)" "$(LD)" "$(NM)" "$(RM)" "$(MV)" "$(@)";
+	"$(OBJDUMP)" "$(OBJCOPY)" "$(CC) $(KBUILD_CFLAGS)" \
+	"$(LD) $(ld_flags)" "$(NM)" "$(RM)" "$(MV)" "$(@)";
 endif
 
 define rule_cc_o_c
author
#1 lore raw
Quoted 5 lines · Paul Mundt

Presently there doesn't seem to be any way to determine whether the target is big or little endian, and it is assumed that the compiler will do the right thing by default. Unfortunately this can not be assumed, and mismatches ensue, resulting in the linker bailing out.

Quoted 9 lines

The only obvious solution I saw was to pass in KBUILD_CFLAGS and ld_flags along with $(CC) and $(LD) to the script, and killing off the hardcoded flags. This at least gets things building, but that still leaves objcopy and objdump as the odd ones out. On the other hand, the format can be figured out by objdumping the object and reading in the file format line, but people obviously do not have consistent naming for these, and a double-pass would be needed -- once for establishing little or big, followed by figuring out which set of regexes to use.

This is roughly what I had in mind for the objdump test, perhaps something like this could be reimplemented by someone who knows something about perl?

---

scripts/recordmcount.pl +23 -4
index c67cec8..177f4ee 100755
@@ -143,6 +143,20 @@ if ($arch eq "x86") {
     }
 }
 
+open(IN, "$objdump -p $inputfile|") || die "error running $objdump";
+
+my $endian = "little";
+
+while (<IN>) {
+	next if /^(\s)*$/;
+	s/^.*file format //g;
+	if (/big/) {
+		$endian = "big";
+	}
+	last;
+}
+close(IN);
+
 if ($arch eq "x86_64") {
     $section_regex = "Disassembly of section\\s+(\\S+):";
     $function_regex = "^([0-9a-fA-F]+)\\s+<(.*?)>:";
@@ -174,10 +188,15 @@ if ($arch eq "x86_64") {
     $type = ".long";
 
     # force flags for this arch
-    $ld .= " -m shlelf_linux";
-    $objcopy .= " -O elf32-sh-linux";
-    $cc .= " -m32";
-
+    if ($endian eq "big") {
+	    $ld .= " -m shelf_linux";
+	    $objcopy .= " -O elf32-shbig-linux";
+	    $cc .= " -mb";
+    } else {
+            $ld .= " -m shlelf_linux";
+            $objcopy .= " -O elf32-sh-linux";
+            $cc .= " -ml";
+    }
 } else {
     die "Arch $arch is not supported with CONFIG_FTRACE_MCOUNT_RECORD";
 }
#2 lore raw
Quoted 32 lines · Paul Mundt
Quoted 5 lines · Paul Mundt

Presently there doesn't seem to be any way to determine whether the target is big or little endian, and it is assumed that the compiler will do the right thing by default. Unfortunately this can not be assumed, and mismatches ensue, resulting in the linker bailing out.

Quoted 9 lines

The only obvious solution I saw was to pass in KBUILD_CFLAGS and ld_flags along with $(CC) and $(LD) to the script, and killing off the hardcoded flags. This at least gets things building, but that still leaves objcopy and objdump as the odd ones out. On the other hand, the format can be figured out by objdumping the object and reading in the file format line, but people obviously do not have consistent naming for these, and a double-pass would be needed -- once for establishing little or big, followed by figuring out which set of regexes to use.

This is roughly what I had in mind for the objdump test, perhaps something like this could be reimplemented by someone who knows something about perl?

---

scripts/recordmcount.pl +3 -0
index c67cec8..177f4ee 100755
@@ -143,6 +143,20 @@ if ($arch eq "x86") {
     }
 }
 
+open(IN, "$objdump -p $inputfile|") || die "error running $objdump";
+
+my $endian = "little";

scripts/recordmcount.pl if ($arch eq "x86") { · line 158

Quoted 10 lines
scripts/recordmcount.pl +10 -0
+
+while (<IN>) {
+	next if /^(\s)*$/;
+	s/^.*file format //g;
+	if (/big/) {
+		$endian = "big";
+	}
+	last;
+}
+close(IN);

I'll turn this into a function and only run it on those archs that need it. This is executed on all C files, and we do not want to punish those archs that have a single endian.

-- Steve

Quoted 26 lines
scripts/recordmcount.pl +10 -4
+
 if ($arch eq "x86_64") {
     $section_regex = "Disassembly of section\\s+(\\S+):";
     $function_regex = "^([0-9a-fA-F]+)\\s+<(.*?)>:";
@@ -174,10 +188,15 @@ if ($arch eq "x86_64") {
     $type = ".long";
 
     # force flags for this arch
-    $ld .= " -m shlelf_linux";
-    $objcopy .= " -O elf32-sh-linux";
-    $cc .= " -m32";
-
+    if ($endian eq "big") {
+	    $ld .= " -m shelf_linux";
+	    $objcopy .= " -O elf32-shbig-linux";
+	    $cc .= " -mb";
+    } else {
+            $ld .= " -m shlelf_linux";
+            $objcopy .= " -O elf32-sh-linux";
+            $cc .= " -ml";
+    }
 } else {
     die "Arch $arch is not supported with CONFIG_FTRACE_MCOUNT_RECORD";
 }
#3 lore raw
In reply to Paul Mundt
Quoted 14 lines · Paul Mundt

Presently there doesn't seem to be any way to determine whether the target is big or little endian, and it is assumed that the compiler will do the right thing by default. Unfortunately this can not be assumed, and mismatches ensue, resulting in the linker bailing out.

The only obvious solution I saw was to pass in KBUILD_CFLAGS and ld_flags along with $(CC) and $(LD) to the script, and killing off the hardcoded flags. This at least gets things building, but that still leaves objcopy and objdump as the odd ones out. On the other hand, the format can be figured out by objdumping the object and reading in the file format line, but people obviously do not have consistent naming for these, and a double-pass would be needed -- once for establishing little or big, followed by figuring out which set of regexes to use.

Quoted 2 lines

The CONFIG_64BIT test could likewise be adopted for testing endianness, but not all architectures have config options for endian selections.

But we could add this - no?
Much better than executing objdump one thousand times.

Sam

#4 lore raw
Quoted 15 lines · Sam Ravnborg
Quoted 14 lines · Paul Mundt

Presently there doesn't seem to be any way to determine whether the target is big or little endian, and it is assumed that the compiler will do the right thing by default. Unfortunately this can not be assumed, and mismatches ensue, resulting in the linker bailing out.

The only obvious solution I saw was to pass in KBUILD_CFLAGS and ld_flags along with $(CC) and $(LD) to the script, and killing off the hardcoded flags. This at least gets things building, but that still leaves objcopy and objdump as the odd ones out. On the other hand, the format can be figured out by objdumping the object and reading in the file format line, but people obviously do not have consistent naming for these, and a double-pass would be needed -- once for establishing little or big, followed by figuring out which set of regexes to use.

Quoted 4 lines
Quoted 2 lines

The CONFIG_64BIT test could likewise be adopted for testing endianness, but not all architectures have config options for endian selections.

But we could add this - no?
Much better than executing objdump one thousand times.

Adding a parameter would be better.

-- Steve

#5 lore raw
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

The CONFIG_64BIT test could likewise be adopted for testing endianness, but not all architectures have config options for endian selections.

But we could add this - no?
Much better than executing objdump one thousand times.

Adding a parameter would be better.

Which moves the problem to the caller which is OK. And then we are back to the same discussion and again we should introduce a CONFIG_ symbol for it.

Sam