ld-script.el 5.8 KB
Newer Older
Masatake YAMATO's avatar
Masatake YAMATO committed
1 2
;;; ld-script.el --- GNU linker script editing mode for Emacs

Nick Roberts's avatar
Nick Roberts committed
3 4
;; Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006
;; Free Software Foundation, Inc.
Masatake YAMATO's avatar
Masatake YAMATO committed
5 6 7 8

;; Author: Masatake YAMATO<jet@gyve.org>
;; Keywords: languages, faces

9 10
;; This file is part of GNU Emacs.

Masatake YAMATO's avatar
Masatake YAMATO committed
11 12 13 14 15 16 17 18 19 20 21 22
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program; see the file COPYING.  If not, write to the
Lute Kamstra's avatar
Lute Kamstra committed
23 24
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
Masatake YAMATO's avatar
Masatake YAMATO committed
25

26 27 28 29 30
;;; Commentary:

;; Major mode for editing GNU linker (ld) scripts.

;;; Code:
Masatake YAMATO's avatar
Masatake YAMATO committed
31 32 33 34 35 36 37

;; Custom
(defgroup ld-script nil
  "GNU linker script code editing commands for Emacs."
  :prefix "ld-script-"
  :group 'languages)

38 39
(defvar ld-script-location-counter-face 'ld-script-location-counter)
(defface ld-script-location-counter
40
  '((t :weight bold :inherit font-lock-builtin-face))
Masatake YAMATO's avatar
Masatake YAMATO committed
41 42 43 44
  "Face for location counter in GNU ld script."
  :group 'ld-script)

;; Syntax rules
45
(defvar ld-script-mode-syntax-table
Masatake YAMATO's avatar
Masatake YAMATO committed
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
  (let ((st (make-syntax-table)))
    (modify-syntax-entry ?\  "-"   st)
    (modify-syntax-entry ?{ "(}" st)
    (modify-syntax-entry ?} "){" st)
    (modify-syntax-entry ?\( "()" st)
    (modify-syntax-entry ?\) ")(" st)
    (modify-syntax-entry ?\[ "(]" st)
    (modify-syntax-entry ?\] ")[" st)
    (modify-syntax-entry ?_ "w"   st)
    (modify-syntax-entry ?. "_"   st)
    (modify-syntax-entry ?\\  "\\" st)
    (modify-syntax-entry ?: "." st)
    (modify-syntax-entry ?, "." st)
    (modify-syntax-entry ?? "." st)
    (modify-syntax-entry ?= "." st)
    (modify-syntax-entry ?* ". 23" st)
    (modify-syntax-entry ?/ ". 14" st)
    (modify-syntax-entry ?+ "." st)
    (modify-syntax-entry ?- "." st)
    (modify-syntax-entry ?! "." st)
    (modify-syntax-entry ?~ "." st)
    (modify-syntax-entry ?% "." st)
    (modify-syntax-entry ?< "." st)
    (modify-syntax-entry ?> "." st)
    (modify-syntax-entry ?& "." st)
    (modify-syntax-entry ?| "." st)
    (modify-syntax-entry ?\" "\"" st)
    st)
  "Syntax table used while in `ld-script-mode'.")

;; Font lock keywords
77
;; (The section number comes from ld's info.)
78
(defvar ld-script-keywords
79 80 81 82 83 84
  '(
    ;; 3.4.1 Setting the Entry Point
    "ENTRY" 
    ;; 3.4.2 Commands Dealing with Files
    "INCLUDE" "INPUT" "GROUP" "AS_NEEDED" "OUTPUT" "SEARCH_DIR" "STARTUP"
    ;; 3.4.3 Commands Dealing with Object File Formats
Masatake YAMATO's avatar
Masatake YAMATO committed
85
    "OUTPUT_FORMAT" "TARGET"
86 87 88 89
    ;; 3.4.3 Other Linker Script Commands
    "ASSERT" "EXTERN" "FORCE_COMMON_ALLOCATION" 
    "INHIBIT_COMMON_ALLOCATION" "NOCROSSREFS" "OUTPUT_ARCH"
    ;; 3.5.2 PROVIDE
90
    "PROVIDE"
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
    ;; 3.5.3 PROVIDE_HIDDEN
    "PROVIDE_HIDEN"
    ;; 3.6 SECTIONS Command
    "SECTIONS" 
    ;; 3.6.4.2 Input Section Wildcard Patterns
    "SORT" "SORT_BY_NAME" "SORT_BY_ALIGNMENT"
    ;; 3.6.4.3 Input Section for Common Symbols
    "COMMON"
    ;; 3.6.4.4 Input Section and Garbage Collection
    "KEEP"
    ;; 3.6.5 Output Section Data
    "BYTE" "SHORT" "LONG" "QUAD" "SQUAD" "FILL"
    ;; 3.6.6 Output Section Keywords
    "CREATE_OBJECT_SYMBOLS" "CONSTRUCTORS"
    "__CTOR_LIST__" "__CTOR_END__" "__DTOR_LIST__" "__DTOR_END__"
    ;; 3.6.7 Output Section Discarding
    ;; See `ld-script-font-lock-keywords'
    ;; 3.6.8.1 Output Section Type
Masatake YAMATO's avatar
Masatake YAMATO committed
109
    "NOLOAD" "DSECT" "COPY" "INFO" "OVERLAY"
110
    ;; 3.6.8.2 Output Section LMA
Masatake YAMATO's avatar
Masatake YAMATO committed
111
    "AT"
112 113 114 115 116
    ;; 3.6.8.4 Forced Input Alignment
    "SUBALIGN"
    ;; 3.6.8.6 Output Section Phdr
    ":PHDR"
    ;; 3.7 MEMORY Command
Masatake YAMATO's avatar
Masatake YAMATO committed
117
    "MEMORY"
118
    ;; 3.8 PHDRS Command
Masatake YAMATO's avatar
Masatake YAMATO committed
119 120
    "PHDRS" "FILEHDR" "FLAGS"
    "PT_NULL" "PT_LOAD" "PT_DYNAMIC" "PT_INTERP" "PT_NONE" "PT_SHLIB" "PT_PHDR"
121
    ;; 3.9 VERSION Command
Masatake YAMATO's avatar
Masatake YAMATO committed
122 123 124
    "VERSION")
  "Keywords used of GNU ld script.")

125
;; 3.10.8 Builtin Functions
126 127
(defvar ld-script-builtins
  '("ABSOLUTE"
Masatake YAMATO's avatar
Masatake YAMATO committed
128 129 130
    "ADDR"
    "ALIGN"
    "BLOCK"
131 132 133
    "DATA_SEGMENT_ALIGN"
    "DATA_SEGMENT_END"
    "DATA_SEGMENT_RELRO_END"
Masatake YAMATO's avatar
Masatake YAMATO committed
134
    "DEFINED"
135
    "LENGTH" "len" "l"
Masatake YAMATO's avatar
Masatake YAMATO committed
136 137 138 139
    "LOADADDR"
    "MAX"
    "MIN"
    "NEXT"
140
    "ORIGIN" "org" "o"
141
    "SEGMENT_START"
Masatake YAMATO's avatar
Masatake YAMATO committed
142 143 144 145 146 147
    "SIZEOF"
    "SIZEOF_HEADERS"
    "sizeof_headers")
  "Builtin functions of GNU ld script.")

(defvar ld-script-font-lock-keywords
148 149 150 151 152
  (append
   `((,(regexp-opt ld-script-keywords 'words)
      1 font-lock-keyword-face)
     (,(regexp-opt ld-script-builtins 'words)
      1 font-lock-builtin-face)
153 154 155 156
     ;; 3.6.7 Output Section Discarding
     ;; 3.6.4.1 Input Section Basics
     ;; 3.6.8.6 Output Section Phdr
     ("/DISCARD/\\|EXCLUDE_FILE\\|:NONE" . font-lock-warning-face)
157 158 159
     ("\\W\\(\\.\\)\\W" 1 ld-script-location-counter-face)
     )
   cpp-font-lock-keywords)
160
  "Default font-lock-keywords for `ld-script-mode'.")
Masatake YAMATO's avatar
Masatake YAMATO committed
161

162 163 164 165
;; Linux-2.6.9 uses some different suffix for linker scripts:
;; "ld", "lds", "lds.S", "lds.in", "ld.script", and "ld.script.balo".
;; eCos uses "ld" and "ldi".
;; Netbsd uses "ldscript.*".
Masatake YAMATO's avatar
Masatake YAMATO committed
166
;;;###autoload
167
(add-to-list 'auto-mode-alist '("\\.ld[si]?\\>" . ld-script-mode))
168
(add-to-list 'auto-mode-alist '("ld\\.?script\\>" . ld-script-mode))
169

170
;;;###autoload
171
(add-to-list 'auto-mode-alist '("\\.x[bdsru]?[cn]?\\'" . ld-script-mode))
Masatake YAMATO's avatar
Masatake YAMATO committed
172 173 174 175 176 177

;;;###autoload
(define-derived-mode ld-script-mode nil "LD-Script"
   "A major mode to edit GNU ld script files"
  (set (make-local-variable 'comment-start) "/* ")
  (set (make-local-variable 'comment-end)   " */")
178 179
  (set (make-local-variable 'font-lock-defaults)
       '(ld-script-font-lock-keywords nil)))
Masatake YAMATO's avatar
Masatake YAMATO committed
180

181 182
(provide 'ld-script)

Masatake YAMATO's avatar
Masatake YAMATO committed
183
;; arch-tag: 83280b6b-e6fc-4d00-a630-922d7aec5593
Masatake YAMATO's avatar
Masatake YAMATO committed
184
;;; ld-script.el ends here