<commit_before>/* ###
 * IP: GHIDRA
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ghidra.app.util.bin.format.elf.relocation;

import ghidra.app.util.bin.format.elf.*;
import ghidra.program.model.address.Address;
import ghidra.program.model.listing.Function;
import ghidra.program.model.listing.Program;
import ghidra.program.model.mem.*;
import ghidra.util.exception.NotFoundException;

public class ARM_ElfRelocationHandler extends ElfRelocationHandler {

	@Override
	public boolean canRelocate(ElfHeader elf) {
		return elf.e_machine() == ElfConstants.EM_ARM;
	}

	@Override
	public void relocate(ElfRelocationContext elfRelocationContext, ElfRelocation relocation,
			Address relocationAddress) throws MemoryAccessException, NotFoundException {

		ElfHeader elf = elfRelocationContext.getElfHeader();
		if (elf.e_machine() != ElfConstants.EM_ARM) {
			return;
		}

		Program program = elfRelocationContext.getProgram();

		Memory memory = program.getMemory();
		
		boolean instructionBigEndian = program.getLanguage().getLanguageDescription().getInstructionEndian().isBigEndian();		
		
		int type = relocation.getType();
		if (type == ARM_ElfRelocationConstants.R_ARM_NONE) {
			return;
		}
		int symbolIndex = relocation.getSymbolIndex();

		long addend = relocation.getAddend(); // will be 0 for REL case

		ElfSymbol sym = elfRelocationContext.getSymbol(symbolIndex);
		String symbolName = sym.getNameAsString();

		boolean isThumb = isThumb(sym);

		long offset = (int) relocationAddress.getOffset();

		long symbolValue = elfRelocationContext.getSymbolValue(sym);

		int newValue = 0;

		switch (type) {
			case ARM_ElfRelocationConstants.R_ARM_PC24: { // Target class: ARM Instruction
				int oldValue = memory.getInt(relocationAddress, instructionBigEndian);
				newValue = (int) (symbolValue + addend);
				newValue -= (offset + 8);  // PC relative, PC will be 8 bytes after inst start
				if (isThumb) {
					newValue |= 1;
				}
				// is this a BLX instruction, must put the lower half word in bit24
				if ((oldValue & 0xf0000000) == 0xf0000000) {
					newValue = (oldValue & 0xfe000000) | (((newValue >> 1) & 1) << 24) |
						((newValue >> 2) & 0x00ffffff);
				}
				else {
					newValue = (oldValue & 0xff000000) | ((newValue >> 2) & 0x00ffffff);
				}
				memory.setInt(relocationAddress, newValue, instructionBigEndian);
				break;
			}
			case ARM_ElfRelocationConstants.R_ARM_ABS32: { // Target class: Data
				int oldValue = memory.getInt(relocationAddress);
				newValue = (int) (symbolValue + addend + oldValue);
				if (isThumb) {
					newValue |= 1;
				}
				memory.setInt(relocationAddress, newValue);
				break;
			}
			case ARM_ElfRelocationConstants.R_ARM_REL32: { // // Target class: Data
				newValue = (int) (symbolValue + addend);
				newValue -= (offset + 8);  // PC relative, PC will be 8 bytes after inst start
				if (isThumb) {
					newValue |= 1;
				}
				memory.setInt(relocationAddress, newValue);
				break;
			}
			case ARM_ElfRelocationConstants.R_ARM_LDR_PC_G0: { // Target class: ARM Instruction
				int oldValue = memory.getInt(relocationAddress, instructionBigEndian);
				newValue = (int) (symbolValue + addend);
				newValue -= (offset + 8);  // PC relative, PC will be 8 bytes after inst start
				newValue = (oldValue & 0xff7ff000) | ((~(newValue >> 31) & 1) << 23) |
					((newValue >> 2) & 0xfff);
				memory.setInt(relocationAddress, newValue, instructionBigEndian);
				break;<commit_msg>GP-164 Corrected ELF ARM R_ARM_REL32 (type 3) relocation processing<commit_after>/* ###
 * IP: GHIDRA
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ghidra.app.util.bin.format.elf.relocation;

import ghidra.app.util.bin.format.elf.*;
import ghidra.program.model.address.Address;
import ghidra.program.model.listing.Function;
import ghidra.program.model.listing.Program;
import ghidra.program.model.mem.*;
import ghidra.util.exception.NotFoundException;

public class ARM_ElfRelocationHandler extends ElfRelocationHandler {

	@Override
	public boolean canRelocate(ElfHeader elf) {
		return elf.e_machine() == ElfConstants.EM_ARM;
	}

	@Override
	public void relocate(ElfRelocationContext elfRelocationContext, ElfRelocation relocation,
			Address relocationAddress) throws MemoryAccessException, NotFoundException {

		ElfHeader elf = elfRelocationContext.getElfHeader();
		if (elf.e_machine() != ElfConstants.EM_ARM) {
			return;
		}

		Program program = elfRelocationContext.getProgram();

		Memory memory = program.getMemory();
		
		boolean instructionBigEndian = program.getLanguage().getLanguageDescription().getInstructionEndian().isBigEndian();		
		
		int type = relocation.getType();
		if (type == ARM_ElfRelocationConstants.R_ARM_NONE) {
			return;
		}
		int symbolIndex = relocation.getSymbolIndex();

		long addend = relocation.getAddend(); // will be 0 for REL case

		ElfSymbol sym = elfRelocationContext.getSymbol(symbolIndex);
		String symbolName = sym.getNameAsString();

		boolean isThumb = isThumb(sym);

		long offset = (int) relocationAddress.getOffset();

		long symbolValue = elfRelocationContext.getSymbolValue(sym);

		int newValue = 0;

		switch (type) {
			case ARM_ElfRelocationConstants.R_ARM_PC24: { // Target class: ARM Instruction
				int oldValue = memory.getInt(relocationAddress, instructionBigEndian);
				newValue = (int) (symbolValue + addend);
				newValue -= (offset + 8);  // PC relative, PC will be 8 bytes after inst start
				if (isThumb) {
					newValue |= 1;
				}
				// is this a BLX instruction, must put the lower half word in bit24
				if ((oldValue & 0xf0000000) == 0xf0000000) {
					newValue = (oldValue & 0xfe000000) | (((newValue >> 1) & 1) << 24) |
						((newValue >> 2) & 0x00ffffff);
				}
				else {
					newValue = (oldValue & 0xff000000) | ((newValue >> 2) & 0x00ffffff);
				}
				memory.setInt(relocationAddress, newValue, instructionBigEndian);
				break;
			}
			case ARM_ElfRelocationConstants.R_ARM_ABS32: { // Target class: Data
				int oldValue = memory.getInt(relocationAddress);
				newValue = (int) (symbolValue + addend + oldValue);
				if (isThumb) {
					newValue |= 1;
				}
				memory.setInt(relocationAddress, newValue);
				break;
			}
			case ARM_ElfRelocationConstants.R_ARM_REL32: { // Target class: Data
				int oldValue = memory.getInt(relocationAddress);
				newValue = (int) (symbolValue + addend + oldValue);
				newValue -= offset;  // PC relative
				if (isThumb) {
					newValue |= 1;
				}
				memory.setInt(relocationAddress, newValue);
				break;
			}
			case ARM_ElfRelocationConstants.R_ARM_LDR_PC_G0: { // Target class: ARM Instruction
				int oldValue = memory.getInt(relocationAddress, instructionBigEndian);
				newValue = (int) (symbolValue + addend);
				newValue -= (offset + 8);  // PC relative, PC will be 8 bytes after inst start
				newValue = (oldValue & 0xff7ff000) | ((~(newValue >> 31) & 1) << 23) |
					((newValue >> 2) & 0xfff);
				memory.setInt(relocationAddress, newValue, instructionBigEndian);
				break;
{'fraction_non_alphanumeric': 0.10780159521435694, 'fraction_numerical': 0.019940179461615155, 'mean_word_length': 3.8695388349514563, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1700698', 'n_tokens_mistral': 2937, 'n_tokens_neox': 2431, 'n_words': 672}