Skip to content

Commit

Permalink
delocate: accept more directive expressions
Browse files Browse the repository at this point in the history
For example, openssl/asm_base.h expands to include the line

  .long ((1 << 0) | (1 << 1));

when BTI and PAC are enabled.

Change-Id: I07208e0430757721e97b88c706672375f8f58f1f
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/62525
Reviewed-by: David Benjamin <davidben@google.com>
Commit-Queue: David Benjamin <davidben@google.com>
(cherry picked from commit ab45f42e8e7340df605f378ee03c4800db2709f3)
  • Loading branch information
scweng authored and torben-hansen committed Jan 30, 2024
1 parent c12cc40 commit ee478f2
Show file tree
Hide file tree
Showing 5 changed files with 2,498 additions and 2,523 deletions.
71 changes: 55 additions & 16 deletions util/fipstools/delocate/delocate.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,47 @@ func (d *delocation) processDirective(statement, directive *node32) (*node32, er
return statement, nil
}

func (d *delocation) processSymbolExpr(expr *node32, b *strings.Builder) bool {
changed := false
assertNodeType(expr, ruleSymbolExpr)

for expr != nil {
atom := expr.up
assertNodeType(atom, ruleSymbolAtom)

for term := atom.up; term != nil; term = skipWS(term.next) {
if term.pegRule == ruleSymbolExpr {
changed = d.processSymbolExpr(term, b) || changed
continue
}

if term.pegRule != ruleLocalSymbol {
b.WriteString(d.contents(term))
continue
}

oldSymbol := d.contents(term)
newSymbol := d.mapLocalSymbol(oldSymbol)
if newSymbol != oldSymbol {
changed = true
}

b.WriteString(newSymbol)
}

next := skipWS(atom.next)
if next == nil {
break
}
assertNodeType(next, ruleSymbolOperator)
b.WriteString(d.contents(next))
next = skipWS(next.next)
assertNodeType(next, ruleSymbolExpr)
expr = next
}
return changed
}

func (d *delocation) processLabelContainingDirective(statement, directive *node32) (*node32, error) {
// The symbols within directives need to be mapped so that local
// symbols in two different .s inputs don't collide.
Expand All @@ -317,24 +358,12 @@ func (d *delocation) processLabelContainingDirective(statement, directive *node3
for node = skipWS(node.up); node != nil; node = skipWS(node.next) {
assertNodeType(node, ruleSymbolArg)
arg := node.up
var mapped string
assertNodeType(arg, ruleSymbolExpr)

for term := arg; term != nil; term = term.next {
if term.pegRule != ruleLocalSymbol {
mapped += d.contents(term)
continue
}

oldSymbol := d.contents(term)
newSymbol := d.mapLocalSymbol(oldSymbol)
if newSymbol != oldSymbol {
changed = true
}

mapped += newSymbol
}
var b strings.Builder
changed = d.processSymbolExpr(arg, &b) || changed

args = append(args, mapped)
args = append(args, b.String())
}

if !changed {
Expand Down Expand Up @@ -1722,6 +1751,16 @@ func writeAarch64Function(w stringWriter, funcName string, writeContents func(st
w.WriteString(".type " + funcName + ", @function\n")
w.WriteString(funcName + ":\n")
w.WriteString(".cfi_startproc\n")
// We insert a landing pad (`bti c` instruction) unconditionally at the beginning of
// every generated function so that they can be called indirectly (with `blr` or
// `br x16/x17`). The instruction is encoded in the HINT space as `hint #34` and is
// a no-op on machines or program states not supporting BTI (Branch Target Identification).
// None of the generated function bodies call other functions (with bl or blr), so we only
// insert a landing pad instead of signing and validating $lr with `paciasp` and `autiasp`.
// Normally we would also generate a .note.gnu.property section to annotate the assembly
// file as BTI-compatible, but if the input assembly files are BTI-compatible, they should
// already have those sections so there is no need to add an extra one ourselves.
w.WriteString("\thint #34 // bti c\n")
writeContents(w)
w.WriteString(".cfi_endproc\n")
w.WriteString(".size " + funcName + ", .-" + funcName + "\n")
Expand Down
16 changes: 6 additions & 10 deletions util/fipstools/delocate/delocate.peg
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,14 @@ QuotedText <- (EscapedChar / [^"])*
LabelContainingDirective <- LabelContainingDirectiveName WS SymbolArgs
LabelContainingDirectiveName <- ".xword" / ".word" / ".long" / ".set" / ".byte" / ".8byte" / ".4byte" / ".quad" / ".tc" / ".localentry" / ".size" / ".type" / ".uleb128" / ".sleb128"
SymbolArgs <- SymbolArg ((WS? ',' WS?) SymbolArg)*
SymbolShift <- ('<<' / '>>') WS? [0-9]+
SymbolArg <- (OpenParen WS?)? (
Offset /
SymbolType /
(Offset / LocalSymbol / SymbolName / Dot) (WS? Operator WS? (Offset / LocalSymbol / SymbolName))* /
LocalLabelRef WS? Operator WS? LocalLabelRef /
LocalSymbol TCMarker? /
SymbolName Offset /
SymbolName TCMarker?)
(WS? CloseParen)? (WS? SymbolShift)?
SymbolArg <- SymbolExpr
SymbolExpr <- SymbolAtom (WS? SymbolOperator WS? SymbolExpr)?
SymbolAtom <- Offset / SymbolType / LocalSymbol TCMarker? / SymbolName Offset / SymbolName TCMarker? / Dot / OpenParen WS? SymbolExpr WS? CloseParen
SymbolOperator <- '+' / '-' / '|' / '<<' / '>>'

OpenParen <- '('
CloseParen <- ')'

SymbolType <- [@%] ('function' / 'object')
Dot <- '.'
TCMarker <- '[TC]'
Expand Down
Loading

0 comments on commit ee478f2

Please sign in to comment.