[PATCH] D30812: AsmPrinter: Don't treat symbols with prefix data as code

Moritz Angermann via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 10 01:00:19 PST 2017


angerman created this revision.
Herald added a subscriber: aemerson.
Herald added a reviewer: javed.absar.

Doing so may result in these symbols being relocated via means of trampoline,
which breaks references to prefix data.

This can be demonstrated with the following setup:

- libtest.ll -------------------------------------------------------------------

define i32 @hello() prefix i32 42 {

  ret i32 42

}
-

- main.c -----------------------------------------------------------------------

#include <stdio.h>

int hello(void);

int main() {

  int *prefix_data = (int*) &hello;
  printf("hi: %d\n", prefix_data[-1]);
  return 0;

}
-

- Makefile --------------------------------------------------------------------

all : libtest.s main

%.s : %.ll
	llc -o $@ $+

libtest.so : libtest.o
	gcc -shared -o $@ $+

main : main.o libtest.so
	gcc -fPIC -L. -ltest -o $@ $+

run : main
	LD_LIBRARY_PATH=. ./main

clean :

git clean -f
------------

$ make run # should result in “hi: 42”

will result in `hi: <random>` on ARMv7 and Aarch64.


https://reviews.llvm.org/D30812

Files:
  lib/CodeGen/AsmPrinter/AsmPrinter.cpp
  test/CodeGen/AArch64/prefixdata-symbol-type.ll
  test/CodeGen/ARM/prefixdata-symbol-type.ll
  test/CodeGen/X86/prefixdata-symbol-type.ll


Index: test/CodeGen/X86/prefixdata-symbol-type.ll
===================================================================
--- /dev/null
+++ test/CodeGen/X86/prefixdata-symbol-type.ll
@@ -0,0 +1,14 @@
+; RUN: llc < %s -mtriple=x86_64-pc-linux | FileCheck %s
+
+; functions with prefix data, should be of type
+; object to prevent them being relocated through
+; the PLT and making the prefix data inaccessable.
+; CHECK: .type f, at object
+define i32 @f() prefix i32 42 {
+  ret i32 0;
+}
+
+; CHECK .type g, at function
+define i32 @g() {
+  ret i32 0;
+}
\ No newline at end of file
Index: test/CodeGen/ARM/prefixdata-symbol-type.ll
===================================================================
--- /dev/null
+++ test/CodeGen/ARM/prefixdata-symbol-type.ll
@@ -0,0 +1,14 @@
+; RUN: llc < %s -mtriple=armv7-pc-linux | FileCheck %s
+
+; functions with prefix data, should be of type
+; object to prevent them being relocated through
+; the PLT and making the prefix data inaccessable.
+; CHECK: .type f,%object
+define i32 @f() prefix i32 42 {
+  ret i32 0;
+}
+
+; CHECK .type g,%function
+define i32 @g() {
+  ret i32 0;
+}
\ No newline at end of file
Index: test/CodeGen/AArch64/prefixdata-symbol-type.ll
===================================================================
--- /dev/null
+++ test/CodeGen/AArch64/prefixdata-symbol-type.ll
@@ -0,0 +1,14 @@
+; RUN: llc < %s -mtriple=aarch64-pc-linux | FileCheck %s
+
+; functions with prefix data, should be of type
+; object to prevent them being relocated through
+; the PLT and making the prefix data inaccessable.
+; CHECK: .type f, at object
+define i32 @f() prefix i32 42 {
+  ret i32 0;
+}
+
+; CHECK .type g, at function
+define i32 @g() {
+  ret i32 0;
+}
\ No newline at end of file
Index: lib/CodeGen/AsmPrinter/AsmPrinter.cpp
===================================================================
--- lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -636,8 +636,18 @@
   if (MAI->hasFunctionAlignment())
     EmitAlignment(MF->getAlignment(), F);
 
-  if (MAI->hasDotTypeDotSizeDirective())
-    OutStreamer->EmitSymbolAttribute(CurrentFnSym, MCSA_ELF_TypeFunction);
+  if (MAI->hasDotTypeDotSizeDirective()) {
+    // We can't treat symbols with prefix data as functions since these
+    // will be relocated via trampoline, which will break references to
+    // prefix data. This can be observed on ELF, and rectified by treating
+    // the symbol as object rather than function.
+    //
+    // For Mach-O, the use of MCSA_AltEntry is used to prevent the
+    // breaking linker from stripping the prefix data.
+    MCSymbolAttr attr = F->hasPrefixData() ? MCSA_ELF_TypeObject
+                                           : MCSA_ELF_TypeFunction;
+    OutStreamer->EmitSymbolAttribute(CurrentFnSym, attr);
+  }
 
   if (isVerbose()) {
     F->printAsOperand(OutStreamer->GetCommentOS(),


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D30812.91280.patch
Type: text/x-patch
Size: 2874 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170310/a0d1d49a/attachment-0001.bin>


More information about the llvm-commits mailing list