Path: blob/main/contrib/llvm-project/lld/ELF/Arch/MSP430.cpp
34889 views
//===- MSP430.cpp ---------------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// The MSP430 is a 16-bit microcontroller RISC architecture. The instruction set9// has only 27 core instructions orthogonally augmented with a variety10// of addressing modes for source and destination operands. Entire address space11// of MSP430 is 64KB (the extended MSP430X architecture is not considered here).12// A typical MSP430 MCU has several kilobytes of RAM and ROM, plenty13// of peripherals and is generally optimized for a low power consumption.14//15//===----------------------------------------------------------------------===//1617#include "Symbols.h"18#include "Target.h"19#include "lld/Common/ErrorHandler.h"20#include "llvm/BinaryFormat/ELF.h"21#include "llvm/Support/Endian.h"2223using namespace llvm;24using namespace llvm::object;25using namespace llvm::support::endian;26using namespace llvm::ELF;27using namespace lld;28using namespace lld::elf;2930namespace {31class MSP430 final : public TargetInfo {32public:33MSP430();34RelExpr getRelExpr(RelType type, const Symbol &s,35const uint8_t *loc) const override;36void relocate(uint8_t *loc, const Relocation &rel,37uint64_t val) const override;38};39} // namespace4041MSP430::MSP430() {42// mov.b #0, r343trapInstr = {0x43, 0x43, 0x43, 0x43};44}4546RelExpr MSP430::getRelExpr(RelType type, const Symbol &s,47const uint8_t *loc) const {48switch (type) {49case R_MSP430_10_PCREL:50case R_MSP430_16_PCREL:51case R_MSP430_16_PCREL_BYTE:52case R_MSP430_2X_PCREL:53case R_MSP430_RL_PCREL:54case R_MSP430_SYM_DIFF:55return R_PC;56default:57return R_ABS;58}59}6061void MSP430::relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const {62switch (rel.type) {63case R_MSP430_8:64checkIntUInt(loc, val, 8, rel);65*loc = val;66break;67case R_MSP430_16:68case R_MSP430_16_PCREL:69case R_MSP430_16_BYTE:70case R_MSP430_16_PCREL_BYTE:71checkIntUInt(loc, val, 16, rel);72write16le(loc, val);73break;74case R_MSP430_32:75checkIntUInt(loc, val, 32, rel);76write32le(loc, val);77break;78case R_MSP430_10_PCREL: {79int16_t offset = ((int16_t)val >> 1) - 1;80checkInt(loc, offset, 10, rel);81write16le(loc, (read16le(loc) & 0xFC00) | (offset & 0x3FF));82break;83}84default:85error(getErrorLocation(loc) + "unrecognized relocation " +86toString(rel.type));87}88}8990TargetInfo *elf::getMSP430TargetInfo() {91static MSP430 target;92return ⌖93}949596